-1

当我运行这个 Haskell 代码时,我遇到了这个非常烦人的错误。由于某种原因,它不喜欢拳头线delLast :: (Ord a) => [a] -> [a]

delLast :: (Ord a) => [a] -> [a]
delLast [] = []
delLast (x:[]) = []
delLast (x:xs) = [x] ++ delLast xs

有谁知道我为什么会收到这个错误?谢谢。

这是我得到的错误:

parse error on input `='
Failed, modules loaded: none.
4

2 回答 2

12

在该行之前是否有未终止的定义?

x = do

delLast :: (Ord a) => [a] -> [a]
delLast [] = []
delLast (x:[]) = []
delLast (x:xs) = [x] ++ delLast xs

编译给了我:

test.hs:5:12: parse error on input `='
于 2012-04-16T06:26:12.970 回答
1

关于什么

delLast [] = []
delLast xs = init xs

或者如果你想按照自己的方式做,但更好

delLast :: (Ord a) => [a] -> [a]
delLast [] = []  
delLast [x] = []                  -- Looks better IMO
delLast (x:xs) = x : delLast xs   -- : instead of ++ gives a performance boost
于 2012-08-07T09:29:32.677 回答