【发布时间】:2012-06-25 11:47:12
【问题描述】:
使用以下延续单子:
type ContinuationMonad() =
member this.Bind (m, f) = fun c -> m (fun a -> f a c)
member this.Return x = fun k -> k x
let cont = ContinuationMonad()
我不明白为什么下面会出现堆栈溢出:
let map f xs =
let rec map xs =
cont {
match xs with
| [] -> return []
| x :: xs ->
let! xs = map xs
return f x :: xs
}
map xs id;;
let q = [1..100000] |> map ((+) 1)
虽然以下没有:
let map f xs =
let rec map xs =
cont {
match xs with
| [] -> return []
| x :: xs ->
let! v = fun g -> g(f x)
let! xs = map xs
return v :: xs
}
map xs id;;
let q = [1..100000] |> map ((+) 1)
【问题讨论】:
-
请注意,我使用的是 VS 2012 RC,如果有人可以测试它在当前版本的 VS2010 上具有相同的行为。
-
是的,它在 OCaml 中也有相同的行为。请参阅下面的答案。
-
FWIW,在 VS2015、F# 4.0、Update 3 中仍然可以观察到这种行为(尽管答案表明它不能归咎于编译器)。
标签: f# monads computation-expression