在 Elm 中定义嵌套或递归列表结构

Define a nested or recursive list structure in Elm

我正在尝试使用 Haskell 99 Questions. In question 7 you have to define a nested list structure. I've tried this: (based somewhat on reading this)

学习 Elm
type NestedList a = Node a | List (NestedList a)

myList : NestedList number
myList =
  [Node 1]

但是我收到以下错误:

The type annotation is saying:
  NestedList number

But I am inferring that the definition has this type:
  List (NestedList number)

这对我来说没有意义。肯定 List (NestedList number)Node a | List (NestedList a) 的第二面相匹配?

问题 #7 要求您使用内置 Elm List 类型作为 NestedList 定义的一部分,但您定义 NestedList 类型的方式实际上创建了一个名为 List 的构造函数无意中隐藏了内置的 List 类型。我认为这种类型签名实际上会给你你想要的东西:

type NestedList a = Node a | NestedList (List (NestedList a))

您的 myList 签名现在应该更改,因为它实际上应该 return 一个 List of NestedLists:

myList : List (NestedList number)
myList =
  [Node 1]  

根据这个新定义,您可以解决 #7 所要求的嵌套问题。您可以像这样定义更复杂的列表:

-- e.g. [1,2, [3, 4, [5]], 6]
myListierList : List (NestedList number)
myListierList =
  [Node 1, Node 2, NestedList [Node 3, Node 4, NestedList [Node 5]], Node 6]