Assignment versus binding

Rustom Mody rustompmody at gmail.com
Wed Oct 5 09:13:53 EDT 2016


On Wednesday, October 5, 2016 at 4:24:01 PM UTC+5:30, BartC wrote:
> On 05/10/2016 11:03, Ben Bacarisse wrote:
> > Gregory Ewing  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)
> 
> (I downloaded Haskell (ghc) yesterday to try this out. First problem was 
> that I couldn't figure out how to do two things one after another 
> (apparently you need 'do').
> 
> And when I tried to use a random number function in an expression to see 
> if it was evaluated once or twice, apparently my installation doesn't 
> have any form of random() (despite being a monstrous 1700MB with 20,000 
> files).

In general this may be true.
In general you need to use cabal or stack in haskell-world like you use
pip in python world to get what stuff you need

However for Random it seems to be part of System:
https://hackage.haskell.org/package/random-1.1/docs/System-Random.html


Here’s a session just blindly following
https://www.schoolofhaskell.com/school/starting-with-haskell/libraries-and-frameworks/randoms

$ ghci 
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> import System.Random
Prelude System.Random> import Control.Monad (replicateM)
Prelude System.Random Control.Monad> let main = replicateM 10 (randomIO :: IO Float) >>= print
Prelude System.Random Control.Monad> main
[0.57468385,0.84545535,0.1624645,0.8491243,0.48347688,2.9622138e-2,0.70125,0.9189069,0.5903369,0.26639372]

Small modif from the link above:
I changed
main = replicateM...
to
let main = replicateM...

for reasons quite related to this whole long thread! viz.
= ie definitions goes into haskell files
At the repl one can only write expressions
let wangles around that

> 
> Not an easy language..)

I am not going to argue that!
Every language (that Ive ever used) has oddities that tripped me up.
Once these initial hurdles are crossed is it hard or easy is a real
but different question.



More information about the Python-list mailing list