[Haskell-beginners] f . g or f g or f $ g?
Patrick Mylund Nielsen
haskell at patrickmylund.com
Tue Feb 12 22:43:37 CET 2013
Exactly. It is a convenience operator that lets you do:
f x $ g y $ h z
f x (g y (h z))
As for whether you should write:
foo = f . g
foo x = f $ g x
..go with whichever looks clearest and nicest to you. In most cases where
you are just taking one argument and applying some functions to it, it's
nice to omit the x. But in more complex cases, it may make your code harder
to read and modify. See http://www.haskell.org/haskellwiki/Pointfree (and
Problems with "pointless" style.)
I personally always use pointfree where I would've required a lambda
f (g . h) y
f (\x -> g $ h x) y
On Tue, Feb 12, 2013 at 10:23 PM, Emanuel Koczwara <
poczta at emanuelkoczwara.pl> wrote:
> Dnia 2013-02-12, wto o godzinie 22:09 +0100, Martin Drautzburg pisze:
> > On Friday, 1. February 2013 23:02:39 Ertugrul Söylemez wrote:
> > > (f . g) x = f (g x)
> > so (f . g) x = f $ g x
> > right?
> > That looks like the two are pretty interchangeable. When would I prefer
> > over the other?
> ($) has lower precedence (it was introduced for that reason I belive).
> Prelude> :info ($)
> ($) :: (a -> b) -> a -> b -- Defined in GHC.Base
> infixr 0 $
> Please take a look at:
> From the docs:
> "Application operator. This operator is redundant, since ordinary
> application (f x) means the same as (f $ x). However, $ has low,
> right-associative binding precedence, so it sometimes allows parentheses
> to be omitted..."
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners