Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Please help me to understand: ($ 3) (Costello, Roger L.)
   2. Re:  Please help me to understand: ($ 3) (Denis Kasak)
   3. Re:  Please help me to understand: ($ 3) (Brandon Allbery)


----------------------------------------------------------------------

Message: 1
Date: Sat, 4 May 2013 17:33:49 +0000
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] Please help me to understand: ($ 3)
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi Folks,

The type signature of ($) is:

        ($) :: (a -> b) -> a -> b

That tells me that ($) is a function that takes two arguments:

        1. A function which maps values of type "a" to values of type "b"
        2. A value of type "a"

and it returns a value of type "b".

Okay, I can understand that.

So, I created some examples:

        ($) 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? 

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?

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?

/Roger




------------------------------

Message: 2
Date: Sat, 4 May 2013 19:41:36 +0200
From: Denis Kasak <[email protected]>
Subject: Re: [Haskell-beginners] Please help me to understand: ($ 3)
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CANJrnZf42K9x1NsqdwJROXEo9JOzeTWSyeEERZ4o2Cp=vn2...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On 4 May 2013 19:33, Costello, Roger L. <[email protected]> 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-0001.htm>

------------------------------

Message: 3
Date: Sat, 4 May 2013 13:46:33 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Please help me to understand: ($ 3)
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <cakfcl4w-najmzpth1r7uihhfo78zgynuuheukv0zxzzytax...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sat, May 4, 2013 at 1:33 PM, Costello, Roger L. <[email protected]>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
operations.


>         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
[email protected]                                  [email protected]
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-0001.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 59, Issue 5
****************************************

Reply via email to