[Haskell-beginners] Recursion in monad

Adrian May adrian.alexander.may at gmail.com
Tue Mar 22 05:46:54 CET 2011


OK I did this:

import System.Random

walk :: Int -> IO Int
walk i = randomRIO (0,1) >>= \r -> return (i+r*2-1)

say :: Int -> IO ()
say i = putStrLn $ show i

rep :: Int -> a -> (a -> IO a) -> (a -> IO ()) -> IO ()
rep n i w s
      | n<=0 = return ()
      | otherwise = s i >> w i >>= \ii -> rep (n-1) ii w s

main :: IO ()
main = rep 10 50 walk say

Is that the easiest way?

Adrian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110322/72654dd7/attachment.htm>


More information about the Beginners mailing list