[Haskell-beginners] Function composition with more than 1 parameter

Glurk streborg at hotmail.com
Sat Oct 25 03:00:08 EDT 2008


Hi,

Is it possible to use function composition with a function that takes more 
than 1 parameter ?

For example, I have a function that finds all the matches of an element within 
a list :-

matches x xs = [ m | m <- xs, x == m ]

So, matches 'e' "qwertyee" will return "eee"

I now want a function that will work out how many times the element occurs in 
the list, easily written as :-

howMany x xs = length $ matches x xs

So, howMany 5 [1,2,3,4,5,5,5,5,5,6,6,6,7]
will return 5

However, I'm thinking I want to try to use function composition to write 
something like :-

howMany = length . matches

...which would be nice and clean :)
I thought this would work, as the output of matches is a list, and the input 
required by length is a list.

This doesn't work though, failng with the error :-

Type error in application
*** Expression     : length . matches
*** Term           : length
*** Type           : [b] -> Int
*** Does not match : ([a] -> [a]) -> Int

Which I can't quite make out what it means.
Ok, it's telling me lengt has a type [b] -> Int which makes sense,
but it says it expected a different type, ([a] -> [a]) -> Int
What I can't understand is why it wants something of that type ?

Is it possibly something to do with the higher precedence of function 
application over function composition ?
I've tried various things, like putting brackets around (length . matches)
and changing my function to take in a tuple of (element, list), instead of 2 
parameters (which I thought I had working at some stage ! but which now I 
can't get to work)

Any advice appreciated !

thanks :)




More information about the Beginners mailing list