Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Created June 28, 2012 12:51
Show Gist options
  • Save davidgrenier/3011180 to your computer and use it in GitHub Desktop.
Save davidgrenier/3011180 to your computer and use it in GitHub Desktop.
Option monad
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