我正在尝试使用Haskell 99 Questions来学习榆树。在问题7中,您必须定义嵌套列表结构。我试过这个:(基于阅读this)
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)
的第二面相匹配?
答案 0 :(得分:6)
问题#7要求您使用内置的Elm List
类型作为NestedList
定义的一部分,但您定义NestedList
类型的方式实际上会创建一个构造函数名为List
,无意中隐藏了内置List
类型。我认为这种类型的签名实际上会给你你想要的东西:
type NestedList a = Node a | NestedList (List (NestedList a))
您的myList
签名现在应该更改,因为它应该实际返回List
个NestedList
:
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]