Tower of hanoi, python -> scheme, shows error. What am I missing?

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 ?

Answers


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

The above code doesn't 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))))

The syntactic sugar in Scheme is

(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 #<void>; arguments were: #<void> #<void>


Need Your Help

MS Word VBA late binding still causes a reference to be added in 2010 that prevents 2003 running

vba object binding reference ms-word

I have a Word doc with VBA code that is essentially a form that gets routed from originator then through 3 other people via programmatically emailing as an attachment. This requires the document t...

What is the best way to monitor a group of ebay users in my application?

sql mysql database-design ebay

I have a mysql database with a table of ebay users, and their individual auctions. What I wish to do is to be able to "monitor" certain users, which would basically include retriving all of the auc...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.