【发布时间】:2020-05-11 21:31:46
【问题描述】:
我的目标是通过获取两个流,将它们压缩在一起,颠倒顺序并应用我的差异函数来制作三重而不是双重来理解这些流。
例如,将此函数应用于赔率流 (1,3,5,7...) 和偶数流 (0,2,4,6,8) 将产生 [(0,1 ,-1); (2,3,-1); (4,5,-1)] 等等,但它应该适用于任何流,而不仅仅是赔率和偶数 - 即三元组中的第三个数字并不总是“-1”。
type 'x str = Cons of 'x * ('x strm) | Nil
and 'x strm = unit -> 'x str
let diff : int -> int -> int = fun x y -> x - y
let rec flip_diff : 'x stream -> 'y stream -> ('x * 'y -> 'z) -> ('y * 'x * 'z) stream =
fun x y f -> fun () ->Cons((head y) (head x) (f y x), flip_diff (tail y) (tail x))
我的错误信息(与打字有关)是这样的:
# let rec flip_diff : 'x stream -> 'y stream -> ('x * 'y -> 'z) -> ('y * 'x * 'z) stream =
fun x y f -> fun () ->Cons((head y) (head x) (f y x), flip_diff (tail y) (tail x));;
Error: This expression has type
('x -> 'a -> 'b) stream = unit -> ('x -> 'a -> 'b) str
but an expression was expected of type 'x * ('x -> 'a -> 'b)
老实说,我很难把我的头绕在打字上。使用惰性流处理较小的函数很容易,但分段构建函数然后将其放在 Ocaml 中就更难了。
【问题讨论】:
标签: ocaml