的第n个元素我有一个返回给定的字符串时列表的功能:OCaml的 - 故障打印一览
let input_string_to_list input =
Str.split (Str.regexp "\n") input
我也有应该返回一个列表的第n个元素的函数:
let rec get_nth = function
| h::_, 0 -> h
| h::t, n -> get_nth(t, n - 1)
;;
然后我试图打印列表的第n个元素(应该是一个字符串):
print_string (get_nth (input_to_set_strings input) n)
但是我得到这个错误在编译:
Error: This expression has type string list but an expression was expected of type ('a -> 'b) list * int
我不理解,我已经溜了,我是相当新的函数式编程,任何帮助表示赞赏,感谢。
'List.nth'存在于stdlib中,顺便说一句。 – gsg