class - Haskell : Working with monad classes -



class - Haskell : Working with monad classes -

as assignment need work monads in haskell , create gambling game has 1 simple rule: toss 6 coins, count heads, roll dice , if result equal or greater amount of heads counted win, otherwise lose. given next 'framework' defining gambling monad:

data coin = h | t deriving (bounded, eq, enum, ord, show) info dice = d1 | d2 | d3 | d4 | d5 | d6 deriving (bounded, eq, enum, ord, show) info outcome = win | lose deriving (eq, ord, show) class monad m => monadgamble m toss :: m coin roll :: m dice game :: monadgamble m => m outcome game = undefined

however i'm still new monads , have no thought how work them. example: game definition should implement game explained above, how should work gambling monad to, example, execute 1 or multiple toss(es)/roll(s) , obtain resulting value(s) can use/work them?

also understanding monad has 2 default functions on it: homecoming , (>>=), don't see how apply monadgable monad?

if can help me out on it's much appreciated!

best regards, skyfe.

firstly, monadgamble not technically monad here, typeclass extending monad has 2 things associated it: toss , roll, each signifying value of toss or roll respectively. in type signature of game, m monad, , instance of monadgamble, automatically have available toss , roll.

you can utilize haskell's notation here. won't go much detail don't want entire assignment, here's how write monad tests if 2 coin tosses come same:

twoflips :: monadgamble m => m bool twoflips = coin1 <- toss coin2 <- toss homecoming (coin1 == coin2)

you might find useful replicatem function control.monad, allows repeat monadic action , homecoming results in list:

import control.monad (replicatem) tencoins :: monadgamble m => m [coin] tencoins = replicatem 10 toss

class haskell monads

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -