1
我試圖解決這種類型的equatin:e^x + sqrt(x)= d,當d已知時。它沒有解析解,所以我使用二進制搜索的變化來解決這個問題:Haskell錯誤消息期待多一個參數
helper x = exp x + sqrt x
ex2 c0 c1 x
| abs (h0 - h1) < 10 ^^ (-6) = c0
| hm < x = ex2 m c1 x
| hm >= x = ex2 c0 m x
where h0 = helper c0
h1 = helper c1
m = c0 + (c1 - c0)/2
hm = helper m
這工作得很好,從ghci中(C0和C1是分鐘和搜索最大值),但我有問題,閱讀從標準輸入輸出參數x :
main = do
seed <- getLine
let output = show ex2 0 6 (read seed :: Floating) -- Result is somewhere between helper(0) and helper(6)
in putStrLn output
這打破了我的代碼。它不會編譯或加載ghci。我得到這個錯誤消息:
ex2.hs:14:46: 期待一個更參數「浮動」 預期一個類型,但「浮動」具有一種「* - >約束」 在表達式簽名:浮動 在「秀」的第四個參數,即「(讀種子::浮動)」 在表達:秀EX2 0 6(閱讀種子::浮動)
有人可以解釋這意味着什麼,以及如何修復我的主要功能?
'Floating'是一個類型類,參見http://hackage.haskell.org/package/base-4.7.0.2/docs/Prelude.html#t:Floating 所以編譯器是正確的,你需要指定你想要的類型的實例。 – Yuras 2015-01-15 18:38:42
我也嘗試將「Floating」改爲「Float」或「Double」,但後來我得到了更多的錯誤。 – happytomato 2015-01-15 18:44:43
將'Floating'更改爲'Float'或'Double',並將putStrLn輸出中的'put'改爲'putStrLn輸出',並將其與'let'對齊到相同的縮進級別。 – user2407038 2015-01-15 18:45:14