[Haskell-beginners] Please help me to understand: ($ 3)

Brandon Allbery allbery.b at gmail.com
Sat May 4 19:46:33 CEST 2013

On Sat, May 4, 2013 at 1:33 PM, Costello, Roger L. <costello at mitre.org>wrote:

>         ($) odd 3       -- returns True
>         odd $ 3         -- returns True
> Good.
> But then I saw this in an article:
>         ($ 3) odd
> What does ($ 3) mean? I thought the first argument to ($) is a function?

If you have an infix operator, you can turn it into a function by wrapping
it in parens, as you know. But you can also go further: you can include one
of the parameters in the parens, producing a partially applied function;
Haskell calls this a section.

($ 3) is a section which has partially applied the right-hand parameter to
($), resulting in a function which expects the left-hand parameter (the
function).  If you were writing this out "longhand", it would be

    \x -> x $ 3

Similarly, (+ 3) is a section on (+) (and (3 +) is the opposite section;
since (+) is commutative, it is indistinguishable from (+3) in practice.
($) is not commutative, since one parameter is a function and the other is
a value to apply the function to, so (x $) and ($ x) are different

>         let list=[1,2,3]
>         (map list) odd
> But that fails. Why? Why does that fail whereas a very similar looking
> form succeeds when ($) is used?

map is not infix, so that means something different; you have simply
applied the first parameter, but that wants to be a function, not a list.
If you rephrase it as infix:

    (`map` list) odd -- `foo` is the function foo as an infix, that is, (x
`foo` y) is (foo x y). note ` not ' !

it will work.

brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130504/5c2286fa/attachment.htm>

More information about the Beginners mailing list