【发布时间】:2020-02-23 05:52:17
【问题描述】:
给定一个列表 [1, 2] 我正在尝试创建一个匿名函数来生成一个 (下一个值 * 生成下一个值的函数) ...类似于
f = createFunc([1; 2])
(v1, f1) = f() //v1 = Some 1
(v2, f2) = f1() //v2 = Some 2
(v3, f3) = f2() //v3 = None
我可以在 Elixir 中轻松做到这一点,但在 F# 中我的头撞到了墙上。任何指点都感激不尽。到目前为止,我的尝试看起来像:
let top_and_tail (l: list<'a>): (Option<'a> * list<'a>) =
match l with
| [] -> (None, [])
| h :: t -> ((Some h), t)
let rec createFun l =
fun() ->
let (h, t) = top_and_tail(l)
(h, createFun(t))
【问题讨论】:
-
这个实现有什么问题?
-
我的 fsi (fsharpi) 给了我一个错误类型不匹配。期望 ''a' 但给定了 'unit -> Option * 'a' 类型 ''a' 和 'unit -> Option * 'a' 不能统一。
-
你试过注释
createFun的类型吗?