[Haskell-cafe] Exercise in point free-style
Julien Oster
haskell at lists.julien-oster.de
Fri Sep 1 20:26:08 EDT 2006
Udo Stenzel wrote:
Thank you all a lot for helping me, it's amazing how quickly I received
these detailed answers!
> func2 f g l = filter f (map g l)
> func2 f g = (filter f) . (map g) -- definition of (.)
> func2 f g = ((.) (filter f)) (map g) -- desugaring
> func2 f = ((.) (filter f)) . map -- definition of (.)
> func2 f = flip (.) map ((.) (filter f)) -- desugaring, def. of flip
> func2 = flip (.) map . (.) . filter -- def. of (.), twice
> func2 = (. map) . (.) . filter -- add back some sugar
Aaaah. After learning from Neil's answer and from @pl that (.) is just
another infix function, too (well, what else should it be, but it wasn't
clear to me) I still wasn't able to come up with that solution without
hurting my brain. The desugaring was the bit that was missing. Thanks, I
will keep that in mind for other infix functions as well.
I tried to work it out on paper again, without looking at your posting
while doing it. I did almost the same thing, however, I did not use
flip. Instead the last few steps read:
= ((.) (filter f)) . map g l
= (.)((.) . filter f)(map) g l -- desugaring
= (.map)((.) . filter f) g l -- sweeten up
= (.map) . (.) . filter g l -- definition of (.)
I guess that's possible as well?
> The general process is called "lambda elimination" and can be done
> mechanically. Ask Goole for "Unlambda", the not-quite-serious
> programming language; since it's missing the lambda, its manual explains
> lambda elimination in some detail. I think, all that's needed is flip,
> (.) and liftM2.
Will do, thank you!
Cheers,
Julien
More information about the Haskell-Cafe
mailing list