Last active
September 15, 2019 08:57
-
-
Save shinkwhek/ef98c152da9b9443b7856dde2e03aeda to your computer and use it in GitHub Desktop.
state monad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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