當我嘗試提取數字的前兩位數字時,出現令人沮喪的錯誤。首先,我使用日誌計算出數字的長度,然後將數字除以10^(length of digit - 2)
以提取前兩位數字。 len和divisor函數工作正常,但前兩個函數失敗了,我不明白這一點,因爲它看起來很直觀。我什至嘗試加入{-# LANGUAGE FlexibleContexts #-}
,但沒有。請協助。下面的代碼:提取一個數字的前兩位數字haskell
len n = (+) 1 $ truncate $ logBase 10 n -- length of a digit
divisor n = 10^(len n - 2)
firstTwo n = div n divisor
我得到的錯誤是:
在約束非類型變量參數:Integral (r -> a)
(Use FlexibleContexts to permit this)
When checking that ‘firstTwo’ has the inferred type
firstTwo :: forall a r.
(Floating r, Integral (r -> a), Num a, RealFrac r) =>
(r -> a) -> r -> a
嘗試增加類型簽名以獲得更好的錯誤。 – melpomene
您還需要檢查指數是否定的。換句話說,確保你的號碼至少是2位數字。 – mathk