Skip to content

Instantly share code, notes, and snippets.

@shinkwhek
Last active September 15, 2019 08:57
Show Gist options
  • Save shinkwhek/ef98c152da9b9443b7856dde2e03aeda to your computer and use it in GitHub Desktop.
Save shinkwhek/ef98c152da9b9443b7856dde2e03aeda to your computer and use it in GitHub Desktop.
state monad
open FSharpPlus
type NewList<'a> =
| Nil
| Cons of 'a * NewList<'a>
type Monadic<'a, 'b> =
| Monadic of ('a -> ('b * 'a))
static member Return x = Monadic(fun c -> (x, c))
static member (>>=) (Monadic f, g) =
Monadic(fun cnt ->
let result1, cnt1 = f cnt
let (Monadic h) = g result1
h cnt1)
let cons x xs = Monadic(fun cnt -> (Cons(x, xs), cnt + 1))
let run c (Monadic f) = f c
[<EntryPoint>]
let main _ =
let m =
monad { let! a = cons "a" Nil
let! b = cons "b" a
return b }
let a = run 0 m
printfn "%A" a
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment