[Haskell-cafe] Why do I need a 'do'?

Daniel Peebles pumpkingod at gmail.com
Wed Apr 29 21:44:34 EDT 2009


It mentions a 'do' expression because GHCi implicitly acts like one,
but your problem is that you have [IO Int], not that you don't have a
do. The simplest way to make this work is to do sequence (rollNDice
3). That will turn [IO Int] into IO [Int], which is something you can
work with more easily.

On Wed, Apr 29, 2009 at 9:39 PM, michael rice <nowgate at yahoo.com> wrote:
> Hi,
>
> Why do I need a 'do' in the code below?
>
> Michael
>
> ==================
>
> import System.Random
>
> rollDice :: IO Int
> rollDice = getStdRandom (randomR (1,6))
>
> rollNDice :: Int -> [IO Int]
> rollNDice 0 = []
> rollNDice n = rollDice : rollNDice (n-1)
>
> =================
>
> [michael at localhost ~]$ ghci rnd0
> GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer ... linking ... done.
> Loading package base ... linking ... done.
> [1 of 1] Compiling Main             ( rnd0.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> rollDice
> Loading package old-locale-1.0.0.1 ... linking ... done.
> Loading package old-time-1.0.0.1 ... linking ... done.
> Loading package random-1.0.0.1 ... linking ... done.
> 6
> *Main> rollDice
> 3
> *Main> rollNDice 3
>
> <interactive>:1:0:
>     No instance for (Show (IO Int))
>       arising from a use of `print' at <interactive>:1:0-10
>     Possible fix: add an instance declaration for (Show (IO Int))
>     In a stmt of a 'do' expression: print it
> *Main>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list