
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@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>

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
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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Wed, 29 Apr 2009, michael rice wrote:
==================
import System.Random
rollDice :: IO Int rollDice = getStdRandom (randomR (1,6))
rollNDice :: Int -> [IO Int] rollNDice 0 = [] rollNDice n = rollDice : rollNDice (n-1)
replicateM n rollDice http://www.haskell.org/haskellwiki/Avoiding_IO#State_monad

import System.Random
rollDice :: IO Int rollDice = getStdRandom (randomR (1,6))
rollNDice :: Int -> [IO Int] rollNDice 0 = [] rollNDice n = rollDice : rollNDice (n-1)
replicateM n rollDice
Or, if you want the original idea: sequence (rollNDice 10) Check the type of sequence at Control.Monad. Maurício

On Thu, 30 Apr 2009, Maurício wrote:
import System.Random
rollDice :: IO Int rollDice = getStdRandom (randomR (1,6))
rollNDice :: Int -> [IO Int] rollNDice 0 = [] rollNDice n = rollDice : rollNDice (n-1)
replicateM n rollDice
Or, if you want the original idea:
sequence (rollNDice 10)
Check the type of sequence at Control.Monad.
I'd still propose http://www.haskell.org/haskellwiki/Haskell_programming_tips#Avoid_explicit_r...
participants (4)
-
Daniel Peebles
-
Henning Thielemann
-
Maurício
-
michael rice