Try F# の Web サイトでは、計算式の例が示されています。
type Age =
| PossiblyAlive of int
| NotAlive
type AgeBuilder() =
member this.Bind(x, f) =
match x with
| PossiblyAlive(x) when x >= 0 && x <= 120 -> f(x)
| _ -> NotAlive
member this.Delay(f) = f()
member this.Return(x) = PossiblyAlive x
let age = new AgeBuilder()
let willBeThere (a:int) (y:int) =
age {
let! current = PossiblyAlive a
let! future = PossiblyAlive (current + y)
return future
}
これは、Haskell にある標準の Maybe モナドに少し似ています。
ただし、真の Haskell 形式では、次の 2 行に return を使用したいと思います。
let! current = PossiblyAlive a
let! future = PossiblyAlive (current + y)
することが:
let! current = return a
let! future = return (current + y)
しかし、それは機能しません。私が得る最も近いものは次のとおりです。
let! current = age.Return a
let! future = age.Return (current + y)
しかし、これは汚れているようです。return
計算ビルダー関数を明示的に使用せず に使用する方法はありますか?