RWH,第3章問題5請求的工作我創建了一個函數來測試paldindrome的存在。關於函數的Haskell問題
我寫了這一點,但它不工作
pCheck :: (Eq a) => [a] -> Bool;
pCheck a = take n a == (take n $ reverse a)
where n = floor (length a/2)
我得到這個錯誤,當我嘗試運行它:
No instance for (RealFrac Int)
arising from a use of `floor' at len.hs:13:11-32
Possible fix: add an instance declaration for (RealFrac Int)
In the expression: floor (length a/2)
In the definition of `n': n = floor (length a/2)
In the definition of `pCheck':
pCheck a = take n a == (take n $ reverse a)
where
n = floor (length a/2)
我很困惑我究竟做錯了什麼?我知道一個paldindrome可以用a == reverse a
進行測試,但現在我想以我的方式找到錯誤。
UPDATE:與代碼中的錯誤之一是固定的,通過司法建議,這個問題已經被更新,以反映剩下的問題
一個簡單測試將是:'isPalindrome X =(秀X)==(反向$顯示X)' – 2009-11-30 06:37:13
回覆:Jonno,我不認爲節目,或需要括號。 'isPalindrome = x ==反向x' – 2009-11-30 06:52:40