更新 - 解决方案
感谢 jacobm 的帮助,我想出了一个解决方案。
// Folding Recursion
let reverse_list_3 theList =
List.fold_left (fun element recursive_call -> recursive_call::element) [] theList;;
我正在学习 OCaml(用于类)中的不同递归方式,并且为了一些练习,我正在编写一个函数来使用不同的递归样式来反转列表。
// Forward Recursion
let rec reverse_list_forward theList =
match theList with [] -> [] | (head::tail) -> (reverse_list_1 tail) @ [head];;
// Tail Recursion
let rec reverse_list_tail theList result =
match theList with [] -> result | (head::tail) -> reverse_list_2 tail (head::result);;
现在,我正在尝试编写一个反向函数,List.fold_left
但我被卡住了,无法弄清楚。我将如何使用折叠来编写这个反向函数?
此外,如果有人对函数式编程、不同类型的递归、高阶函数等有很好的参考,将不胜感激链接:)