Hi,
first I like to thank all of you guys - it really helps! I am still on a same chapter with higher order functions and this function is also confusing. But before i even define this function i am getting the type error - i don't know why? So i wrote the simpler one like: filterAlpha :: (a -> Bool) -> [a] -> [a] filterAlpha f [] = [] filterAlpha f (x:xs) |f x = x : filterAlpha xs |otherwise = filterAlpha xs and i am getting this error message: Type error in application Expression :filterAlpha xs Type : [b] Dous not match : a -> Bool To even my very little knowledge i think that this should work. What am i doing wrong? Thanks again |
Alexteslin wrote:
> filterAlpha :: (a -> Bool) -> [a] -> [a] > filterAlpha f [] = [] > filterAlpha f (x:xs) > |f x = x : filterAlpha xs > |otherwise = filterAlpha xs > > > and i am getting this error message: > > Type error in application > Expression :filterAlpha xs > Type : [b] > Dous not match : a -> Bool filterAlpha :: (a -> Bool) -> [a] -> [a] filterAlpha f [] = [] filterAlpha f (x:xs) | f x = x : filterAlpha f xs | otherwise = filterAlpha f xs filterAlpha has two parameters. The first parameter is a function (a -> Bool), the second is a list [a]. The error message complains that xs , which you actidentially gave as first parameter, is a list [a] and not a function (a -> Bool). Regards, apfelmus _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Alexteslin
On Monday 23 July 2007, Alexteslin wrote:
> Hi, > first I like to thank all of you guys - it really helps! > > I am still on a same chapter with higher order functions and this function > is also confusing. > But before i even define this function i am getting the type error - i > don't know why? So i wrote the simpler one like: > > filterAlpha :: (a -> Bool) -> [a] -> [a] > filterAlpha f [] = [] > filterAlpha f (x:xs) > |f x = x : filterAlpha xs > |otherwise = filterAlpha xs > > and i am getting this error message: > > Type error in application > Expression :filterAlpha xs > Type : [b] > Dous not match : a -> Bool > > To even my very little knowledge i think that this should work. What am i > doing wrong? You're passing only one argument to filterApha, when it takes two. Haskell can't figure out which arguments you want to stay the same on every recursive call, and which ones you want to vary, so you have to supply every argument every time. Jonathan Cast http://sourceforge.net/projects/fid-core http://sourceforge.net/projects/fid-emacs _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
In reply to this post by Heinrich Apfelmus
Oh silly me. I defined firstFirst now, thank you. |
In reply to this post by Alexteslin
If you find it tedious to pass parameters that never change, remember
that you can access symbols defined in the enclosing environment (closure), freeing you from "passing it on" each time: filterAlpha :: (a -> Bool) -> [a] -> [a] filterAlpha f = filterAlpha' where filterAlpha' [] = [] filterAlpha' (x:xs) | f x = x : (filterAlpha' xs) | otherwise = (filterAlpha' xs) As far as the primed version filterAlpha' is concerned, f is a global symbol. The argument list is just used for values that vary. This will be your friend if the parameter list starts to stack up with more and more "reference" or "environment" inputs. It is also easier to see that f never changes if it is defined in only one spot. Later, when you study monads you will notice the same pattern in the Reader monad, where the technique is even more valuable. Dan Weston Alexteslin wrote: > > filterAlpha :: (a -> Bool) -> [a] -> [a] > filterAlpha f [] = [] > filterAlpha f (x:xs) > |f x = x : filterAlpha f xs -- corrected > |otherwise = filterAlpha f xs -- corrected _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe |
Thank you Dan
|
Free forum by Nabble | Edit this page |