the ($) function (was desugaring an example from RWH)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

the ($) function (was desugaring an example from RWH)

Michael Easter
re: desugaring.  Thanks Andrew, that worked great!

I have another, easier question from the same code in RWH:

bogusTransfer qty fromBal toBal = do
    fromQty <- atomically $ readTVar fromBal
    [snip]

Can someone please explain the ($) function in English? From the type
signature, it seems to be an "apply function", but I can't quite explain
when we use it. Clearly, it is used throughout RWH but I haven't found a
good explanation.

My guess is that it is when we want an "execution" of a function rather than
a mere reference to it. Is that accurate?

thanks again
Michael

--
----------------------
Michael Easter
http://codetojoy.blogspot.com: Putting the thrill back in blog

http://youtube.com/ocitv -> Fun people doing serious software engineering
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090223/12b940d4/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Re: the ($) function (was desugaring an example from RWH)

Andrew Wagner
The $ function is essentialy a "no-op". That is, it literally does nothing.
There is no difference in Haskell between a function and a reference to it.
The only purpose of $ is for grouping. The line in question below could have
been written identically as "fromQty <- atomically (readTVar fromBal)". The
$ groups together everything to the end of the line, and can be used to
avoid parentheses that could add noise to the code.

On Mon, Feb 23, 2009 at 1:18 PM, Michael Easter <[hidden email]> wrote:

>
> re: desugaring.  Thanks Andrew, that worked great!
>
> I have another, easier question from the same code in RWH:
>
> bogusTransfer qty fromBal toBal = do
>     fromQty <- atomically $ readTVar fromBal
>     [snip]
>
> Can someone please explain the ($) function in English? From the type
> signature, it seems to be an "apply function", but I can't quite explain
> when we use it. Clearly, it is used throughout RWH but I haven't found a
> good explanation.
>
> My guess is that it is when we want an "execution" of a function rather
> than a mere reference to it. Is that accurate?
>
> thanks again
> Michael
>
> --
> ----------------------
> Michael Easter
> http://codetojoy.blogspot.com: Putting the thrill back in blog
>
> http://youtube.com/ocitv -> Fun people doing serious software engineering
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090223/2834b420/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Re: the ($) function (was desugaring an example from RWH)

Peter Verswyvelen-2
Sometimes the apply operator $ can be handy for other purposes too, e.g.
map ($ 1) [cos, sin, tan]

evaluates to

[cos $ 1, sin $ 1, tan $ 1]

which just evaluates to

[cos 1, sin 1, tan 1]



Note that some consider the following as bad coding style:

f $ g $ h $ x

Instead, the following is preferred

f . g . h $ x

On Mon, Feb 23, 2009 at 7:27 PM, Andrew Wagner <[hidden email]>wrote:

> The $ function is essentialy a "no-op". That is, it literally does nothing.
> There is no difference in Haskell between a function and a reference to it.
> The only purpose of $ is for grouping. The line in question below could have
> been written identically as "fromQty <- atomically (readTVar fromBal)". The
> $ groups together everything to the end of the line, and can be used to
> avoid parentheses that could add noise to the code.
>
>
> On Mon, Feb 23, 2009 at 1:18 PM, Michael Easter <[hidden email]>wrote:
>
>>
>> re: desugaring.  Thanks Andrew, that worked great!
>>
>> I have another, easier question from the same code in RWH:
>>
>> bogusTransfer qty fromBal toBal = do
>>     fromQty <- atomically $ readTVar fromBal
>>     [snip]
>>
>> Can someone please explain the ($) function in English? From the type
>> signature, it seems to be an "apply function", but I can't quite explain
>> when we use it. Clearly, it is used throughout RWH but I haven't found a
>> good explanation.
>>
>> My guess is that it is when we want an "execution" of a function rather
>> than a mere reference to it. Is that accurate?
>>
>> thanks again
>> Michael
>>
>> --
>> ----------------------
>> Michael Easter
>> http://codetojoy.blogspot.com: Putting the thrill back in blog
>>
>> http://youtube.com/ocitv -> Fun people doing serious software engineering
>>
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090223/ed10b885/attachment-0001.htm
Reply | Threaded
Open this post in threaded view
|

the ($) function (was desugaring an example from RWH)

John Hartnup
In reply to this post by Michael Easter
I had exactly the same confusion - not explaining ($) in RWH was major
dropped ball IMO.

>
> bogusTransfer qty fromBal toBal = do
>     fromQty <- atomically $ readTVar fromBal
>     [snip]
>

... is equivalent to:
bogusTransfer qty fromBal toBal = do
     fromQty <- atomically ( readTVar fromBal
     [snip]
     )

... so using ($) prevents accumulating a great big set of parentheses
to close at the end of a long block.


--
"There is no way to peace; peace is the way"