haskell - creating a function that takes a list of Ints and returns the list with any odd numbers squared -
i want write function similar filter using odd function takes list , returns list odd numbers squared.
ex
gchi> sqrodd [1,2,3,4,5] [1,2,9,4,25]
what have , believe close is
sqrodd :: (a->bool) -> [a] -> [a] sqrodd odd [] = [] sqrodd odd (x:xs) = if odd x (x*x) :sqrodd odd xs else x : sqrodd odd xs
but errors function definition saying "couldn't match expected type a -> bool
actual type [a]
"
as wrote yourself, want function takes list argument , returns list, instead of having type signature
sqrodd :: (a->bool) -> [a] -> [a]
you should make function type signature looks this
sqrodd :: [a] -> [a]
which have written on beginning. because of compiler expects, first argument of function function of (a -> bool). should remove odd arguments list , change type signature shown above. cause function use odd prelude instead of expecting filter function argument.
another way of doing it, renaming function as, example sqrfiltered , can define sqrodd partially applied sqrfiltered
sqrodd = sqrfiltered odd
not tested, should right.
Comments
Post a Comment