2016-11-07 114 views
0

有人可以幫我嗎?我試圖運行average功能,但我得到的是讀取錯誤「(語法錯誤)上輸入‘::’」此代碼:Haskell顯示解析錯誤

sum` :: [Int] -> Int 
sum` [] = 0 
sum` (x:xs) = x + sum` xs 

length` :: [a] -> Int 
length` [] = 0 
length` (x:xs) = 1 + length` xs 

average :: [Int] -> Rational 
average [] = error "empty list" 
average xs = toRational (sum xs)/toRational(length xs) 

非常感謝

+8

我不認爲你可以使用單引號('''),請嘗試使用單引號(''') –

+0

它的工作原理謝謝 – Kevin

+0

@JosephYoung因爲您的評論解決了這個問題,你可以發佈它作爲一個回答所以可以接受? – sclv

回答

1

按的意見評論,我提出我的一個答案評論:

我不認爲你可以使用反引號(`),請嘗試使用單引號('

所以它會是:

sum' :: [Int] -> Int 
sum' [] = 0 
sum' (x:xs) = x + sum' xs 

length' :: [a] -> Int 
length' [] = 0 
length' (x:xs) = 1 + length' xs 

average :: [Int] -> Rational 
average [] = error "empty list" 
average xs = toRational (sum xs)/toRational(length xs