所以我剛開始學習Haskell,我試圖用這個if語句:我的if有什麼問題?
[if (((mod x 3) == 0) && ((mod x 5) == 0)) then "Fizzbuzz" else x | x <- [1..50]]
,但是當我在ghci中編譯,我得到以下錯誤:
No instance for (Integral [Char])
arising from a use of `mod' at baby.hs:22:19-25
Possible fix: add an instance declaration for (Integral [Char])
In the first argument of `(==)', namely `(mod x 3)'
In the first argument of `(&&)', namely `((mod x 3) == 0)'
In the expression: (((mod x 3) == 0) && ((mod x 5) == 0))
Failed, modules loaded: none.
好了,所以我我們發現x被推斷爲一個字符串,因爲if返回一個顯式字符串,因此這個整個函數將不起作用。那麼我怎麼才能真正解決這個問題呢? (我知道我的問題是愚蠢的,但我不習慣用功能範例或靜態鍵入類型推理)。
什麼是'x'?它似乎是一個'字符串'。 – kennytm 2011-02-25 06:24:24
@KennyTM是正確的。沒有看到更多的代碼,很難幫助你。如果'x'是一個Int,這個代碼就沒問題。 – luqui 2011-02-25 06:36:50
請縮小爲可上傳大小的代碼片段,幷包含錯誤消息(但肯定的,從它的聲音KennyTM在正確的軌道上)。 – 2011-02-25 06:46:58