Haskell- Running monad states -


i stuck trying function:

runarraystate :: array arr => arraystate arr e -> arr e -> (a, arr e) 

to run arraystate action let's call act array arr , result , return result res , original array arr' pair (res,arr').

arraystate defined as

data arraystate arr e = mkarraystate (arr e -> (a, arr e)) 

i thought be:

runarraystate act arr = ((act arr), arr) 

or

runarraystate mkarraystate (\ arr -> (res, arr)) arr' = (res, arr') 

but fails. ideas?

i think

runarraystate (mkarraystate act) arr = act arr 

is looking for.

your type arraystate defined having single constructor mkarraystate has argument function takes arrays pairs consisting of result , (presumably) updated array. in definition above, use identifier act refer function , arr array have input. @ right-hand side of definition, apply function act arr obtain required pair.

alternatively, can define type as

data arraystate arr e = mkarraystate {runarraystate :: arr e -> (a, arr e)} 

or, (most likely) better even,

newtype arraystate arr e = mkarraystate {runarraystate :: arr e -> (a, arr e)} 

that way, have directly defined destructor function of type arraystate arr e -> arr e -> (a, arr e) (i.e., without class constraint, not required in version above either).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -