我想獲得的來自多個數字列表匹配,這裏是我的代碼:麻煩類型在Haskell
digits x = if x > 0 then (i : digits (floor (x/10))) else [i]
where i = (mod x 10)
我得到這個代碼的錯誤是:
No instance for (Integral a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Integral Integer -- Defined in ‘GHC.Real’
instance Integral Int -- Defined in ‘GHC.Real’
instance Integral Word -- Defined in ‘GHC.Real’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
我做錯了什麼?
使用'div'而不是'/'和'floor' – Carsten
''digits x = if x> 0 then(x'mod'10):digits(x' div'10)else [x'mod'10 ] - 請注意,你會得到一個反向列表(在大多數情況下會有一個不想要的'0') – Carsten
這是在某些情況下嘗試使用該函數的GHCi錯誤消息。總是爲你的函數添加類型簽名,這樣問題就會立即顯現,而不是其他地方。 – leftaroundabout