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

Denis Kasak denis.kasak at gmail.com
Sat May 4 19:41:36 CEST 2013


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

<snip>

>
> But then I saw this in an article:
>
>         ($ 3) odd
>
> What does ($ 3) mean? I thought the first argument to ($) is a function?
>
> I checked the type of ($ 3) and it is:
>
>         ($ 3) :: Num a => (a -> b) -> b
>
> I don't understand that. How did that happen? Why can I take a second
> argument and wrap it in parentheses with ($) and then that second argument
> pops out and becomes the argument to a function?
>

These are called operator sections. Take a look at
http://www.haskell.org/haskellwiki/Section_of_an_infix_operator


>
> I decided to see if other functions behaved similarly. Here is the type
> signature for the "map" function:
>
>         map :: (a -> b) -> [a] -> [b]
>
> That looks very similar to the type signature for ($). So, I reasoned, I
> should be able to do the same kind of thing:
>
>         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?
>

Because (map list) is an ordinary function application since operator
sections apply only to infix operators. On the other hand, had you written
(`map` list), it would have worked as you expected.

λ. let list = [1, 2, 3]
λ. (`map` list) odd
[True,False,True]

-- 
Denis Kasak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130504/befbcd16/attachment.htm>


More information about the Beginners mailing list