Saturday 13 December 2008

F# State Monad

//F# State Monad

type State<'state, 'a> = State of ('state ->'a * 'state)

type StateMonad() =
member b.Bind(m, f) = State (fun s -> let r = match m with
| State f -> f s
match r with
| (v,s) -> match f v with
| State f -> f s)
member b.Return(x) = State (fun s -> x, s)

let state = StateMonad()

let GetState = State (fun s -> s, s)
let SetState s = State (fun _ -> (), s)

let Execute m s = match m with
| State f -> let r = f s
match r with
|(x,_) -> x

No comments: