输入`='解析错误 - Haskell

时间:2012-04-16 04:03:14

标签: haskell functional-programming

我在运行这个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.

2 个答案:

答案 0 :(得分: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 `='

答案 1 :(得分: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