Hanoi Tower, python -> scheme, shows an error. What am I missing?

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 correct solution for the tower of Hanoi. But my port circuit,

 (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 correct solution, but at the end causes the following error.

 procedure application: expected procedure, given: #<void>; arguments were: #<void> #<void> 

I know his expression about printing, which throws an error, but I canโ€™t understand why this is happening?

+4
source share
1 answer
  ((move src tmp dst (- num 1)) (move src dst tmp 1) (move tmp dst src (- num 1))) 

The above code does not do what you think it does.

To execute a series of expressions / statements you need something like this:

 ((ฮป () (move src tmp dst (- num 1)) (move src dst tmp 1) (move tmp dst src (- num 1)))) 

Syntactic Sugar in Scheme

 (begin (move ...) (move ...) (move ...) ...) 

 ((move ...) (move ...) (move ...)) 

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 where you get the error message #<void>; arguments were: #<void> #<void> #<void>; arguments were: #<void> #<void>

+4
source

Source: https://habr.com/ru/post/1302742/


All Articles