[Haskell-beginners] f . g or f g or f $ g?
Paul Higham
polygame at mac.com
Wed Feb 13 02:35:20 CET 2013
An example that helped me understand the utility of the $ operator is as follows:
suppose that we have a list of functions
fs :: [a -> b]
fs = [f1, f2, ..., fk]
and a list of arguments
as :: [a]
as = [a1, a2, ..., ak]
and we want to produce the list
[f1(a1), f2(a2), ..., fk(ak)], or equivalently [f1 a1, f2 a2, ..., fk ak],
this can be done using the $ operator via
zipWith ($) fs as
simply because it makes the function application explicit.
::paul
On 2013-02-12, at 13:44 , mukesh tiwari wrote:
> You can write (f . g) x as f . g $ x so for me, it's avoiding extra parenthesis.
>
> Mukesh
>
> On Wed, Feb 13, 2013 at 2:53 AM, Emanuel Koczwara <poczta at emanuelkoczwara.pl> wrote:
> Hi,
>
> 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 one
> > 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:
> http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Prelude.html#v:-36-
>
> 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..."
>
> Emanuel
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130212/d466c238/attachment.htm>
More information about the Beginners
mailing list