河内塔,蟒蛇->方案,显示错误。我缺少什么?
python 实现
import sys
def move(src, dst, tmp, num):
if num == 1: print 'Move from', src, 'to', dst
else:
move(src, tmp, dst, num-1)
move(src, dst, tmp, 1)
move(tmp, dst, src, num-1)
move('left', 'right', 'middle', int(sys.argv[1]))
为河内塔提供了正确的解决方案。但是我的方案端口
(define move
(lambda (src dst tmp num)
(if (= num 1) (printf "Move from ~s to ~s \n" src dst)
((move src tmp dst (- num 1))
(move src dst tmp 1)
(move tmp dst src (- num 1))))))
提供了正确的解决方案,但最终抛出以下错误。
procedure application: expected procedure, given: #<void>; arguments were: #<void> #<void>
我知道是我的 print 语句引发了错误,但我不明白为什么会发生这种情况?
The python implementation
import sys
def move(src, dst, tmp, num):
if num == 1: print 'Move from', src, 'to', dst
else:
move(src, tmp, dst, num-1)
move(src, dst, tmp, 1)
move(tmp, dst, src, num-1)
move('left', 'right', 'middle', int(sys.argv[1]))
Gives the right solution for tower of hanoi. But my scheme port,
(define move
(lambda (src dst tmp num)
(if (= num 1) (printf "Move from ~s to ~s \n" src dst)
((move src tmp dst (- num 1))
(move src dst tmp 1)
(move tmp dst src (- num 1))))))
Gives the right solution but in the end throws the following error.
procedure application: expected procedure, given: #<void>; arguments were: #<void> #<void>
I know that its my print statement that is throwing the error, but I can't figure out why is this happening ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码并没有像你想象的那样:)
要执行一系列表达式/语句,你需要这样的东西:
Scheme中的语法糖
将被评估,并且代码似乎工作。但是一旦递归结束,
解释器将尝试将其执行为
(op param1 param2)
,其中您收到错误
#;参数是:##<无效>
The above code doesn't do what you think it does :)
To execute a series of expressions / statements you need something like this:
The syntactic sugar in Scheme is
will be evaluated, and the code seems to work. But as soon as the recursion ends,
the interpreter will try to execute it as
(op param1 param2)
and there is whereyou get the error
#<void>; arguments were: #<void> #<void>