【发布时间】:2012-09-29 07:08:36
【问题描述】:
编写一个 Ocaml 函数 list_print : string list -> unit 从左到右打印列表中的所有字符串:
所以假设我有一个 Ocaml 函数list_print: string list -> unit,它打印列表中的所有字符串,从左到右写入。现在正确的解决方案是:
let list_print lst = List.fold_left (fun ( ) -> fun s -> print_string s) () lst;;
但是在编写我的解决方案时,我是这样写的:
let list_print lst = List.fold_left (fun s -> print_string s) () lst;;
但这给了我
错误:此表达式的类型为 unit,但表达式的类型应为 'a -> string
为什么我需要第一个参数 fun() -> 在 fun 之前?我还是 Ocaml 的新手,所以这种类型系统让我很困惑
【问题讨论】:
标签: list ocaml fold higher-order-functions