0
雖然學習Haskell我想寫賦予了許多將給其繼任者在Collatz sequence功能:當我運行next 7
我得到使用'print'引起的模糊變量`a0'?
next :: (Fractional a, Integral a) => a -> a
next x
| odd x = x * 3 + 1
| otherwise = x/2
:
<interactive>:150:1: error:
* Ambiguous type variable `a0' arising from a use of `print'
prevents the constraint `(Show a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
instance Show Ordering -- Defined in `GHC.Show'
instance Show Integer -- Defined in `GHC.Show'
instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
...plus 22 others
...plus 12 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In a stmt of an interactive GHCi command: print it
兩個問題:
- 我在我的簽名中使用了最好的類限制嗎?
- 假設我是,我如何
show
next 7
的結果?
沒有類型滿足'(分數a,積分a)'。你想刪除'Fractional'並使用''''''''而不是''''''''' – Alec
謝謝 - 這很有效,我學到了一些新東西。你想發佈一個答案,我可以接受嗎? – urig
我建議使用更具體的類型:'Int - > Int'。 –