下面定义了一个代数数据类型IntList:
data IntList = Empty | Cons Int IntList
函数intListProd计算IntList类型的乘积:
intListProd :: IntList -> Int
intListProd Empty = 1
intListProd (Cons x l) = x * intListProd l
但是我不确定如何创建IntList类型。
我试过了 :
*Main> let x = Cons 3
*Main> :t x
x :: IntList -> IntList
但是如何创建IntList类型以便调用intListProd?
一个Cons
单元格包含两件事:一个元素和一个尾巴。如果列表仅包含一个元素,则尾部为空(由null构造函数表示Empty
)。因此,要表示包含3作为其唯一元素的列表,请使用:
let x = Cons 3 Empty
然后:t x
将IntList
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句