这是一个参考问题是:StackOverflow in continuation monad
与我起了一点,就需要一些澄清。怎样的延迟延续单子正好起到防止计算器?
1)我想这:
member this.Delay(mk) = fun c -> mk() c
使得计算工作流的行为做之探源这些之间显示的toyvo:
cBind (map xs) (fun xs -> cReturn (f x :: xs))
cBind (fun c -> map xs c) (fun xs -> cReturn (f x :: xs))
所以我完全不明白什么是当
(fun c -> map xs c)
只是(map xs)
的不同符号
2)推断ence问题。 - 在OP的第二个地图例子中,我发现它不会编译,因为v
值的推理问题,因为它推断f
为a -> b list
,而不是所需的a -> b
。为什么它以这种方式推断?在let v = f x
的情况下,它会很好地推断。
3)在我看来,VS显示工具提示不准确的类型签名:单子的收益 返回类型是:('e->'f)->f
,而绑定的返回类型只'c->'b
。 - 它似乎它在绑定的情况下简化('e->'f)
只c
,还是我失去了一些东西?
感谢您的澄清,
托马斯
编辑 - 测试转储:
let cReturn x = fun k -> k x
let cBind m f =
printfn "cBind %A" <| m id
fun c -> m (fun a -> f a c)
let map_fixed f xs =
let rec map xs =
printfn "map %A" xs
match xs with
| [] -> cReturn []
| x :: xs -> cBind (fun c -> map xs c) (fun xs -> cReturn (f x :: xs))
map xs (fun x -> x)
let map f xs =
let rec map xs =
printfn "map %A" xs
match xs with
| [] -> cReturn []
| x :: xs -> cBind (map xs) (fun xs -> cReturn (f x :: xs))
map xs (fun x -> x)
[1..2] |> map_fixed ((+) 1) |> printfn "%A"
[1..2] |> map ((+) 1) |> printfn "%A"
MAP_FIXED:
地图[1; 2] 地图[2] 地图[] cBind [] 地图[] cBind [3] 地图[2] 地图[] cBind [] 地图[] [2; 3]
图:
地图[1; 2] map [2] map [] cBind [] cBind [3] [2; 3]
编辑质疑2:
let map f xs =
let rec map xs =
cont {
match xs with
| [] -> return []
| x :: xs ->
let v = f x // Inference ok
//let! v = cont { return f x } // ! Inference issue - question 2
let! xs = map xs
return v :: xs
}
map xs id
关于你的第二个问题,推论似乎对我很好。 – kvb
如果我取消注释// let!..它给了我:类型不匹配。期待一个'一个,但给了'一个列表 – tomasK
我最后看到的错误 - 是在我的绑定定义 - 类似的日志在第1点 - 愚蠢的我,感谢兴趣。 – tomasK