Assignment versus binding

Ben Bacarisse ben.usenet at bsb.me.uk
Wed Oct 5 06:03:44 EDT 2016


Gregory Ewing <greg.ewing at canterbury.ac.nz> writes:

> Steve D'Aprano wrote:
>
>> And (shamelessly using Python syntax) if I have a function:
>>
>> def spam(x):
>>     print(x)
>>     print(x+1)
>>
>> spam(time.sleep(60) or 1)
>
> You can't write that in Haskell, because Haskell's
> equivalent of print() is not a function (or at least
> it's not a function that ever returns), and neither
> is sleep().

I suppose it all depends on what "that" is exactly.  Here is the closest
match I could come up with:

import Control.Concurrent

spam io = do x <- io;
             print x;
             print (x+1)

main = spam (do threadDelay (2*10^6); return 1)

It matches the Python in that the delay happens once.  To get the
behaviour being hinted at (two delays) you need to re-bind the IO
action:

spam io = do x <- io;
             print x;
             x <- io;
             print (x+1)

<snip>
-- 
Ben.



More information about the Python-list mailing list