因此,基本上,我对ym作业有一个疑问:“使用前奏函数创建一个检查列表是否为空的函数”,我看到了null函数,我想使用它并打印消息。所以我尝试了以下方法:
notEmpty :: [Int] -> [Char]
notEmpty [x] = if (null[x]) then "False" else "True"
Run Code Online (Sandbox Code Playgroud)
如果我调用notEmpty [],它会给我这个错误:经过几次学习后,函数notEmpty中的Non.Exhaustive模式出现了:
notEmpty :: [Int] -> [Char]
notEmpty [] = "False"
notEmpty [x] = if (null[x])then "False" else "True"
Run Code Online (Sandbox Code Playgroud)
但是在此之后,我尝试了以下输入:notEmpty [1,2],它给了我同样的错误。
我的问题是,当我运行null [1,2]时,它给我错误,那么我在做什么错呢?
[x]并不意味着“称为列表x”。它的意思是“具有一个元素的列表,其中一个元素称为x”。只需使用x代替[x],您的第一种方法将起作用:
notEmpty :: [Int] -> [Char]
notEmpty x = if (null x) then "False" else "True"
Run Code Online (Sandbox Code Playgroud)