编写一个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;;
Run Code Online (Sandbox Code Playgroud)
但在编写我的解决方案时,我是这样编写的:
let list_print lst = List.fold_left (fun s -> print_string s) () lst;;
Run Code Online (Sandbox Code Playgroud)
但这给了我
错误:此表达式具有类型单位,但表达式需要类型为"a - > string"
为什么在乐趣之前我需要第一个参数fun() - >?我还是Ocaml的新手,所以这种类型的系统让我很困惑
所以我有一个列表,我试图分成两个列表,这样原始列表中的所有偶数索引组成第一个列表,奇数索引组成第二个列表.这是我尝试解决方案,但它没有正确返回:
let rec separate xs =
match xs with
[] -> [],[]
| x::y::xs -> x::separate xs, y::separate xs
| x::[] -> x:: separate [], separate [];;
Run Code Online (Sandbox Code Playgroud)