Haskell:模式中的解析错误:A -1可能是由缺失' do&#x27引起的?
试图实施著名的 ackermann函数在Haskell中实际上是可衡量的,我偶然发现了错误描述的美丽:
模式中解析错误:A -1 可能是由于缺失的“做”引起的?
我知道这个解析错误很常见,我不知道为什么这样做。 除此之外,我的代码(见下文)应该很好。
我的代码:
main :: IO ()
main = do
print "Please enter first operand: "
input <- getLine
let n = read input
print "Please enter second operand: "
input <- getLine
let m = read input
let r = ak(n,m)
print(n++" ackermann "++m++" = "++show r)
ak(a,b) = do
if a == 0
then return (b+1)
else if b == 0
then return ak(a-1, 1)
else
s1 <- ak(a-1, b)
s2 <- ak(a-1, s1)
return s2
While trying to implement the famous Ackermann function in Haskell to test whether the promised difference in running time is actually measurable, I stumbled across this beauty of an error description:
Parse error in pattern: a - 1 Possibly caused by a missing 'do'?
I know that this parsing error is appearing pretty commonly and I have no idea why it does so.
Other than that, my code (see below) should be fine.
My code:
main :: IO ()
main = do
print "Please enter first operand: "
input <- getLine
let n = read input
print "Please enter second operand: "
input <- getLine
let m = read input
let r = ak(n,m)
print(n++" ackermann "++m++" = "++show r)
ak(a,b) = do
if a == 0
then return (b+1)
else if b == 0
then return ak(a-1, 1)
else
s1 <- ak(a-1, b)
s2 <- ak(a-1, s1)
return s2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
do
和返回
ak
。do
块用于 monadic 计算。您可以使用:然后可以实现
main
as:Don't use
do
andreturn
forak
.do
blocks are used for monadic computations. You can work with:Then you can implement
main
as: