【发布时间】:2013-05-31 07:48:39
【问题描述】:
给定一个自定义类型的列表,例如
data List' a = EmptyList | NonEmptyList a (List' a)
deriving Show
以及一个判断此类列表是否为非空的函数
null' xs = case xs of
EmptyList -> True
_ -> False
为什么不在 GHCi 中使用类似列表参数调用它
null' NonEmptyList [1,2,3]
或
null' EmptyList []
工作?调用定义了构造函数的函数确实有效。
null' Emptylist
这是为什么?
【问题讨论】:
-
您希望在这里发生什么?你给
null'两个参数。null'接受一个参数并返回一个列表。 -
for null' function yes, but for append' 正如你所见,我希望它能给我一个附加列表。
-
此外,您似乎正在将内置语法(即带有方括号的内容)与您自己的列表混合在一起。你可以写
NonEmptyList 1 (NonEmptyList 2 (NonEmptyList 3 EmptyList)))来制作你自己的列表。不涉及方括号。 -
我不关注... 你应该准确说明: * 你在做什么; * 你期望什么(也许是为什么); * 发生了什么。
-
IOW 从实现
fromList :: [a] -> List' a开始,也许还可以实现toList :: List' a -> [a]。
标签: haskell functional-programming pattern-matching recursive-datastructures