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

apfelmus apfelmus at quantentunnel.de
Sat Oct 25 05:10:03 EDT 2008


Glurk wrote:
> 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

The lambdabot on #haskell has a plugin named "pointless" that can
transform this into a definition that doesn't mention  x  and  xs
anymore. I think it will propose

  howMany = (length .) . matches

And with

  matches x xs = filter (x==) xs

  matches x    = filter (x==)

  matches      = filter . (==)

we have

  howMany = (length .) . filter . (==)

> 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)

  howMany = curry (length . uncurry matches)


Regards,
apfelmus



More information about the Beginners mailing list