[Haskell-beginners] Re: Function composition with more than 1 parameter
daniel.is.fischer at web.de
Sat Oct 25 10:39:23 EDT 2008
Am Samstag, 25. Oktober 2008 15:57 schrieb Glurk:
> > > I'm using WinHugs - do you get them to work in some other environment ?
> > Did you give a type signature for these?
> > Then you would of course have to include the context Eq b, since (==) is
> > If you don't give any type signatures, all above versions should work in
> > all environments, otherwise it would be a serious bug.
> OK, now I'm getting somwhere ! :)
> I tried it with GHCi, and it works !
> ...but only after disabling the monomorphism restriction !
Oh yes, the dreaded MR I forgot.
> It offered another alternative, which was to give a type definition.
> So, I tried :-
> matches :: Eq a => a -> [a] -> [a]
> matches = filter . (==)
> hm :: Eq a => a -> [a] -> Int
> hm = (length .) . matches
> in Hugs, and that also worked :)
> I'm still struggling to come to grips with how it is actually working...
> I was thinking that this pointfree style was to make things simpler, more
> readable and understandable...but now I'm not so sure !
Sometimes pointfree style is clearer and more readable, sometimes it makes
Get some experience writing both, pointfree and pointful versions, to develop
a feeling when to write pointfree and when not (and when to use mixed style,
e.g. "hm x = length . matches x" might be clearer).
> If it's not a simple case (f.g.h etc) I might just stick with putting the
> parameter names in. At this point, brackets and multiple dots like this
> don't really make things clearer to me (Not to mention John's post below,
> with 3 consecutive dots ! I'm really having trouble with that one !). Of
> course, a lot of this is due to my lack of understanding - I'll keep
> experimenting and trying to learn :)
> In general though, do people find this style clearer ?
> I'd be interested to know what other people prefer.
> Thanks for all the help :)
If you compose single-argument functions, the pointfree
composition = f . g . h
seems clearer than
composition x = f . g . h $ x
, a case like howMany,
howMany = (length .) . matches
is sufficiently clear either way (less if written ((.) . (.)) length matches),
but beyond that supplying arguments is desirable.
Would you prefer to read
composition = ((.) . (.) . (.) . (.) . (.)) func thing
composition = ((((func .) .) .) .) . thing
composition a b c d = func . thing a b c d
composition a b c d e = func $ thing a b c d e
IMO, the last two are okay, the first is baaad, the second bad unless in
More information about the Beginners