Created
June 28, 2012 12:51
-
-
Save davidgrenier/3011180 to your computer and use it in GitHub Desktop.
Option 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
type OptionBuilder() = | |
member this.Return x = Some x | |
member this.Bind(x, f) = | |
match x with | |
| Some x -> f x | |
| None -> None | |
member this.ReturnFrom x = x | |
let option = OptionBuilder() | |
let test = | |
option { | |
let! x = Some 3 | |
let! y = Some 4 | |
return x + y | |
} | |
let test2 = | |
option { | |
return! Some 6 | |
} | |
let test3 = | |
option { | |
let! a = test | |
let! b = test2 | |
return a * b | |
} | |
let test4 = | |
option { | |
let! q = None | |
let! t = test3 | |
return q * t | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment