F# Unclassified State Monad

Source Code Snippet (edit)

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
    
comments powered by Disqus

Last Source Code Snippets in same category

F# Mandelbrot console application fractal

F# Unclassified2011-06-16 -

F# Make a sequence anywhere

F# Unclassified2011-05-19 -

F# C# switch / case

F# Unclassified2011-05-19 -

F# Simple lambda expression

F# Unclassified2011-05-19 -

F# Cast an integer to enum

F# Unclassified2011-05-19 -

F# Some cast usages

F# Unclassified2011-05-19 -