2012-04-16 165 views
-1

當我運行這個Haskell代碼時,我得到了這個非常惱人的錯誤。出於某種原因,它不喜歡的拳頭線delLast :: (Ord a) => [a] -> [a]解析輸入錯誤'=' - Haskell

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

利己人知道爲什麼會即時得到這個錯誤?謝謝。

這是我的錯誤:

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

什麼是錯誤的編譯器輸出? – Attila 2012-04-16 04:10:58

+0

我剛剛添加了輸出錯誤 – cclerville 2012-04-16 04:18:59

+3

您發佈的代碼沒有錯誤。 – 2012-04-16 04:24:11

回答

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

什麼

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