我无法在树中构建元素列表

Dun*_*nno 0 ocaml list

我将树类型定义为

type 'a tree = Nil | Node of ('a tree * 'a * 'a tree)
Run Code Online (Sandbox Code Playgroud)

我需要在这个树中构建一个元素列表.我试过(没有尾递归):

let elements t =
  let rec walk node =
    match node with
    | Nil -> []
    | Node(lChild, x, rChild) ->
      (walk lChild) :: [x] :: (walk rChild)
  in
  walk set
Run Code Online (Sandbox Code Playgroud)

但是我得到一个类型错误:表达式(walk lChild)有类型'a list但是表达式需要类型'a

所以我想我的问题可以简化为"如何将一个元素列表添加到列表中".

谢谢!

ivg*_*ivg 5

你需要@运算符,它连接两个列表.

:: 是一个consing运算符,它只能将一个元素添加到列表中,即它的左侧应该是一个元素,右侧应该是列表.