Task

Solve the   Towers of Hanoi   problem with recursion.




 
#include <hopper.h>
#proto hanoi(_X_,_Y_,_Z_,_W_)
main:
   get arg number (2,discos)
   {discos}!neg? do{fail=0,mov(fail),{"I need a positive (or zero) number here, not: ",fail}println,exit(0)}
   pos? do{
      _hanoi( discos, "A", "B", "C" )
   }
exit(0)
.locals
hanoi(discos,inicio,aux,fin)
   iif( {discos}eqto(1), {inicio, "->", fin, "\n"};print,  _hanoi({discos}minus(1), inicio,fin,aux);\
                                                           {inicio, "->", fin, "\n"};print;\
                                                           _hanoi({discos}minus(1), aux, inicio, fin))
back
 