[Haskell-beginners] nim programme

Chaddaï Fouché chaddai.fouche at gmail.com
Sun Oct 25 17:25:55 EDT 2009


On Sun, Oct 25, 2009 at 8:09 PM, Peter Verswyvelen <bugfact at gmail.com> wrote:
> Btw, it's often a good idea to introduce type signatures:
>
> nim :: IO [Int]
>
> then you don't need to provide type signatures either, and don't have
> to disable the monomo restriction
>

That's the solution I would use in this case. :)

Also, there's too much redundancy in this code, you don't need
getStdRandom, you don't need to repeat 3 times the same snippet, and
so on, this code does the same thing in a arguably clearer fashion and
is unarguably shorter :

> initNim :: IO [Int]
> initNim = replicateM 3 $ randomRIO (1,10)

(you need to import Control.Monad and System.Random for this)

"replicateM n action" just does "action" n times and returns the
results in a list, randomRIO is equal to "getStdRandom . randomR".

All that you want to do is pretty easy to do, as long as you do it a
small bit at a time.

-- 
Jedaï


More information about the Beginners mailing list