[Haskell-beginners] replacing fold with scan!
raffa f
freitasraffa at gmail.com
Thu May 1 02:42:09 UTC 2014
hi everyone! here's my new problem. i wrote my version of filter:
filter' :: (a -> Bool) -> [a] -> [a]
filter' f = foldr (\x acc -> if f x then x:acc else acc) []
and it works! however, i wanted to use scan too. so i just replaced foldr
with scanr, to see what would happen:
filter'' :: (a -> Bool) -> [a] -> [a]
filter'' f = scanr (\x acc -> if f x then x:acc else acc) []
but that doesn't work! ghci gives me this:
folds.hs:15:59:
Couldn't match expected type `a' with actual type `[a0]'
`a' is a rigid type variable bound by
the type signature for filter'' :: (a -> Bool) -> [a] -> [a]
at folds.hs:14:13
In the second argument of `scanr', namely `[]'
In the expression:
scanr (\ x acc -> if f x then x : acc else acc) []
In an equation for filter'':
filter'' f = scanr (\ x acc -> if f x then x : acc else acc) []
Failed, modules loaded: none.
the problem seems to be with the start value of [], it seems? i don't
understand, i thought scan and fold worked pretty much the same. i learned
about these functions today, so i'm still trying to wrap my head around
them...
thank you!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140430/05c04d9d/attachment.html>
More information about the Beginners
mailing list