still random number problem

Hal Daume III hdaume@ISI.EDU
Wed, 24 Jul 2002 12:20:42 -0700 (PDT)


There are a few things wrong with this...

> uni ::  IO () -> Float
> uni  = do
>      xs <-  newStdGen 
>      let
>        m =  (head  (randoms xs) :: Float )

presumably, you want 'uni' to produce a random float.  in this case, it
has the wrong type; it is actually an IO action that returns a Float,
hence it's type should be:

  uni :: IO Float

furthermore, IO actions (and functions in general) need to return
something; since you're using 'do' notation, you need to have a call to
return, something like:

  uni = do xs <- newStdGen
           let m = (head (randoms xs) :: Float)
           return m   -- return the head

or more simply

  uni = do xs <- newStdGen
           return (head (randoms xs))

then, since do { x <- f ; y x } really means "f >>= \x -> y x" which is
"f >>= y", you could write this as

  uni = newStdGen >>= return . head . randoms

(if that doesn't make sense, don't worry)

> doubleit :: Float -> Float
> doubleit n = 2.0*n

this is fine

> main = print (doubleit uni)

here's another problem.  the type of uni is IO Float.  the type of
doubleit is Float -> Float.  You can't pass an IO Float as a parameter
instead of a float.  what you need to do is perform the action uni, get
the result, pass it to doubleit and then print that, something like:

  main = do v <- uni
            print (doubleit v)

again, you can rewrite this:

  main = uni >> print . doubleit

hope that made some sense, i gotta run

 - hal