【发布时间】:2020-09-14 19:34:49
【问题描述】:
我正在尝试遍历流以打印内容。
type 'a stream = Nil | Cons of 'a * 'a stream thunk and 'a thunk = unit -> 'a
这是我的函数被调用的地方
|> iter_stream ~f:(fun (f,c,l) -> printf "%s %s %s\n" f c l)
这就是类型
let rec iter_stream st ~f
(* val iter_stream : 'a stream -> ('a -> unit) -> unit *)
我似乎找不到任何关于如何实现它的示例。我唯一的想法是将其视为一个列表,这显然是错误的,因为我遇到了类型错误。
let rec iter_stream st ~f =
match st with
| None -> ()
| Some(x, st') -> f x; iter_stream st' ~f
【问题讨论】:
-
您可以查看标准库的流模块是如何实现的:github.com/ocaml/ocaml/blob/… Streams 也在 SklMl 中实现。你可以在这里获取源代码:sklml.inria.fr/archive/sklml-2.1+pl0.tgz
标签: functional-programming ocaml