我正在学习Haskell,使用的教材是《为了更好地学习Haskell!》(Learn You a Haskell for Great Good!),但是我犯了一个奇怪的错误,找不到原因。
这是我输入的代码:
let xs = [if x < 3 then "bang" else "boom" | x <- xs]
以下是GHCi中的错误文本:
No instance for (Num [Char])
arising from the literal `3'
Possible fix: add an instance declaration for (Num [Char])
In the second argument of `(<)', namely `(3)'
In the expression: x < (3)
In the expression: if x < (3) then "bang" else "boom"
但是当我输入以下内容时:
let boom xs = [if x < 3 then "bang" else "boom" | x <- xs]
这是书中的例子,我没遇到任何问题。
有人可以解释一下我的错误吗?