【发布时间】:2016-01-02 17:21:27
【问题描述】:
我正在尝试从 OCAML 中的 int 列表创建树。我对函数式编程很陌生。到目前为止,这是我的功能:
let rec theList (lst : int list) =
match lst with
| [] -> Empty
| h::t -> insert Empty h::theList List.tl lst
当 insert 是一个创建节点并将 h 的值放入节点的函数时。我尝试遍历列表,但是在使用参数调用 insert 之后在冒号处出现错误:“错误:变体类型 bstTree 没有构造函数 ::”,因为这是我定义为的类型:
type bstTree = Empty | bstTree * Node of int * bstTree
从广义上讲,我要做的就是递归遍历列表并在列表中的每个 int 上调用 insert。我已经为此工作了一段时间,因此感谢您的帮助,谢谢。
【问题讨论】:
标签: list recursion tree functional-programming ocaml