我试图解决这种等式:当已知d时,e ^ x + sqrt(x)= d。它没有解析解决方案,因此我使用二进制搜索的变体来解决它:
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是搜索的最小值和最大值),但是我在从stdio读取参数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:
期待有更多关于“ float ”的论点
预期为一种类型,但“ float ”类型为“*->约束”
在表达式类型签名中: float
在“显示”的第四个参数中,即“((read seed::Floating)”
在表达式中:show ex2 0 6(读取种子:: float )
有人可以解释一下这是什么意思以及如何修复我的主要功能吗?
最佳答案
let ..行中缺少括号:
另一个错误:show(ex2 ...(read seed::Double))需要括号。 – chi
关于haskell - Haskell错误消息需要另外一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970545/