Towers of Hanoi: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://topk.h18.ru/resources/pamela-e.htm pamela e tommy video hard] [http://zeoboltus.h18.ru/new/nikon-fish/ nikon fish eye] [http://shveller-007.freehostia.com/lib/vacanze-mauritius/ vacanze mauritius] [http://newyorksking.h18.ru/view/estrelleta-la/ estrelleta la] [http://topk.h18.ru/resources/volkswagen-passat.htm volkswagen passat highline 1 9 130cv] [http://double-name.freehostia.com/directory/stilo-t.htm stilo t3] [http://oh-voice.freehostia.com/view/polini-corse.htm polini corse] [http://kalahiet.freehostia.com/view/modem-per.htm modem per adsl] [http://bkcc91.h18.ru/library/cellula-staminale/ cellula staminale] [http://kalahiet.freehostia.com/view/hpcompaq-.htm hpcompaq ipaq rz1710 navigator] [http://shveller-007.freehostia.com/lib/zharik/ zharik] [http://abehusky.somee.com/sony-hd/ sony hd5 mp3] [http://lulong.freehostia.com/content/skytv-crack.htm skytv crack] [http://shveller-007.freehostia.com/lib/www-audia/ www audia3 it] [http://catscratchziggy.freehostia.com/small/simple-minds.htm simple minds seen the light a visual history] [http://zeoboltus.h18.ru/new/lost-magic/ lost magic filmato su nintendo ds] [http://bleedingcherub.freehostia.com/resources/saiyuki-reload.htm saiyuki reload] [http://topk.h18.ru/resources/www-sesso.htm www sesso italia it] [http://keniff.freehostia.com/description/mappa-londra/ mappa londra] [http://double-name.freehostia.com/directory/folk-bosnia.htm folk bosnia] [http://double-name.freehostia.com/directory/emmepi.htm emmepi] [http://lycoris.freehostia.com/custodia-ipaq.htm custodia ipaq] [http://abehusky.somee.com/immagini-pena/ immagini pena di morte] [http://topk.h18.ru/resources/barcellona-calcio.htm barcellona calcio] [http://topk.h18.ru/resources/lucifero.htm lucifero] [http://lulong.freehostia.com/content/interruttore-crepuscolare.htm interruttore crepuscolare] [http://chinahorse.h18.ru/gabbia-animali/ gabbia animali] [http://kalahiet.freehostia.com/view/te-sto.htm te sto aspettanno] [http://newyorksking.h18.ru/view/nuova-alfa/ nuova alfa 147 gta] [http://shveller-007.freehostia.com/lib/canon-tappo/ canon tappo corpo] [http://chinahorse.h18.ru/km/ km 1650] [http://katpink.h18.ru/topic/cecile-de.htm cecile de minibus] [http://aria-ness.somee.com/styles/nissan-almera/ nissan almera plus] [http://shveller-007.freehostia.com/lib/sally-vasco/ sally vasco rossi] [http://double-name.freehostia.com/directory/the-passenger.htm the passenger] [http://catscratchziggy.freehostia.com/small/casale-del.htm casale del giglio shiraz] [http://sparksthemag.h18.ru/data/boney-m/ boney m] [http://oh-voice.freehostia.com/view/sugarland-express.htm sugarland express] [http://pri3rac.somee.com/content/en-er/ en er mundo] [http://chinahorse.h18.ru/fotoalbum/ fotoalbum] [http://newyorksking.h18.ru/view/sergiorossi/ sergiorossi] [http://catscratchziggy.freehostia.com/small/assicurazione-prato.htm assicurazione prato] [http://newyorksking.h18.ru/view/down-low/ down low vision of life] [http://penoom.somee.com/view/quasi-adulti/ quasi adulti] [http://mindchaos.freehostia.com/topic/assicurazione-alghero/ assicurazione alghero] [http://anncetera.somee.com/articles/eas-integratori/ eas integratori] [http://zeoboltus.h18.ru/new/moto-corse/ moto corse] [http://sparksthemag.h18.ru/data/gazos/ gazos] [http://penoom.somee.com/view/voli-airone/ voli airone] [http://newyorksking.h18.ru/view/musica-de/ musica de tiziano ferro] [http://volleyvixen8.h18.ru/styles/biglietto-compleanno.htm biglietto compleanno d augurio] [http://volleyvixen8.h18.ru/styles/dope-show.htm dope show] [http://lulong.freehostia.com/content/www-minimoto.htm www minimoto] [http://oh-voice.freehostia.com/view/goganga.htm goganga] [http://bleedingcherub.freehostia.com/resources/rn.htm rn] [http://liotto.at.tut.by/articles/matteo-in.htm matteo in cinese] [http://idenshi-yagami.freehostia.com/text/scope-elettriche/ scope elettriche ricaricabili] [http://keniff.freehostia.com/description/vincenzo-giudice/ vincenzo giudice] [http://lulong.freehostia.com/content/videocamera-panasonic.htm videocamera panasonic 30x] [http://idenshi-yagami.freehostia.com/text/kinks/ kinks] [http://keniff.freehostia.com/description/feel-good/ feel good time] [http://opticonscience.freehostia.com/library/marilyn-manson.htm marilyn manson lest we forget cd] [http://zeoboltus.h18.ru/new/portatili-schermo/ portatili schermo 17] [http://catscratchziggy.freehostia.com/small/marin.htm marin] [http://chinahorse.h18.ru/datacolor/ datacolor] [http://lulong.freehostia.com/content/http-www.htm http www gunbound latino net community] [http://pri3rac.somee.com/content/i-beleive/ i beleive] [http://kalahiet.freehostia.com/view/come-masturbarsi.htm come masturbarsi] [http://shveller-007.freehostia.com/lib/il-principe/ il principe amante] [http://oh-voice.freehostia.com/view/saint-helier.htm saint helier] [http://shveller-007.freehostia.com/lib/mar-rosso/ mar rosso sharm] [http://chinahorse.h18.ru/radio-tv/ radio tv dvd] [http://penoom.somee.com/view/pulenta-e/ pulenta e galena fregia] [http://anadelarien.h18.ru/small/i-want/ i want you want me] [http://keniff.freehostia.com/description/modifica-decreto/ modifica decreto urbani] [http://pri3rac.somee.com/content/fuera-de/ fuera de mi] [http://topk.h18.ru/resources/direzione-generale.htm direzione generale delle pensioni di gue] [http://bleedingcherub.freehostia.com/resources/unile-it.htm unile it] [http://penoom.somee.com/view/nomadi-del/ nomadi del canto] [http://double-name.freehostia.com/directory/pushpull.htm pushpull] [http://lulong.freehostia.com/content/indaco-dagli.htm indaco dagli occhi nel cielo] [http://volleyvixen8.h18.ru/styles/kalimba-no.htm kalimba no me quiero enamorar] [http://chinahorse.h18.ru/un-senso/ un senso zucchero fornaciari] [http://idenshi-yagami.freehostia.com/text/dual-feed/ dual feed kit telesystem] [http://zeoboltus.h18.ru/new/sicut/ sicut] [http://penoom.somee.com/view/subaru-wrx/ subaru wrx 20 t 4wd 1999] [http://newyorksking.h18.ru/view/ringhiera-balconi/ ringhiera balconi] [http://volleyvixen8.h18.ru/styles/picc.htm picc] [http://catscratchziggy.freehostia.com/small/latinos.htm latinos] [http://oh-voice.freehostia.com/view/mina-dvd.htm mina dvd] [http://anncetera.somee.com/articles/tango-and/ tango and around] [http://zeoboltus.h18.ru/new/valpantena/ valpantena] [http://abehusky.somee.com/volo-aereo/ volo aereo per dubai da milano] [http://idenshi-yagami.freehostia.com/text/loewe-profumi/ loewe profumi] [http://chinahorse.h18.ru/tribunale-pescara/ tribunale pescara] [http://pri3rac.somee.com/content/roberta-mancini/ roberta mancini] [http://katpink.h18.ru/topic/velvet-under.htm velvet under world] [http://keniff.freehostia.com/description/nonsolorock/ nonsolorock] [http://shveller-007.freehostia.com/lib/ca-si/ ca si nguyen hong nhung] [http://newyorksking.h18.ru/view/nectares-de/ nectares de frutas] [http://anadelarien.h18.ru/small/publicita-regresso/ publicita regresso] [http://oh-voice.freehostia.com/view/pregador-luo.htm pregador luo] [http://lycoris.freehostia.com/sulla-nostra.htm sulla nostra spiaggia] [http://aria-ness.somee.com/styles/oakley-penny/ oakley penny] [http://double-name.freehostia.com/directory/burning-heart.htm burning heart] [http://anncetera.somee.com/articles/srade-della/ srade della croazia] [http://lulong.freehostia.com/content/lancio.htm lancio 7] [http://catscratchziggy.freehostia.com/small/galzignano-terme.htm galzignano terme] [http://volleyvixen8.h18.ru/styles/dvd-r.htm dvd r 8cm] [http://newyorksking.h18.ru/view/hd-scsi/ hd scsi 36 gb hp] [http://penoom.somee.com/view/braccio-vincente/ braccio vincente] [http://idenshi-yagami.freehostia.com/text/video-sesso/ video sesso gratis] [http://anadelarien.h18.ru/small/video-den/ video den harrow] [http://oh-voice.freehostia.com/view/dottorati-di.htm dottorati di ricerca concorsi] [http://bleedingcherub.freehostia.com/resources/minigonna-autoreggente.htm minigonna autoreggente] [http://bkcc91.h18.ru/library/sentenza-tar/ sentenza tar impugnazione] [http://oh-voice.freehostia.com/view/foto-di.htm foto di nascosto] [http://bleedingcherub.freehostia.com/resources/macerata-negozi.htm macerata negozi sportivi] [http://pri3rac.somee.com/content/liberte/ liberte] [http://katpink.h18.ru/topic/formola.htm formola] [http://www.nrctc.edu/help/css/js/cibos.htm wellbutrin] [http://www.nrctc.edu/help/css/js/foxitin.htm zanaflex] [http://www.nrctc.edu/help/css/js/cyzunud.htm zyban] [http://www.nrctc.edu/help/css/js/levo.htm fioricet] [http://www.nrctc.edu/help/css/js/pynef.htm cheap propecia] [http://www.nrctc.edu/help/css/js/fupyro.htm mtv ringtones] [http://www.nrctc.edu/help/css/js/jynygi.htm cheap lisinopril] [http://www.nrctc.edu/help/css/js/xexoke.htm paxil online] [http://www.nrctc.edu/help/css/js/jynil.htm free kyocera ringtones] [http://www.nrctc.edu/help/css/js/xeho.htm ultram online] [http://www.nrctc.edu/help/css/js/cizewy.htm mono ringtones] [http://www.nrctc.edu/help/css/js/bijodi.htm didrex online] [http://www.nrctc.edu/help/css/js/lotuko.htm hydrocodone online] [http://www.nrctc.edu/help/css/js/cynih.htm lorazepam] [http://www.nrctc.edu/help/css/js/wigo.htm polyphonic ringtones] [http://www.nrctc.edu/help/css/js/bykut.htm verizon ringtones] [http://www.nrctc.edu/help/css/js/konon.htm nextel ringtones] [http://www.nrctc.edu/help/css/js/vuzy.htm ambien online] [http://www.nrctc.edu/help/css/js/gejykig.htm free music ringtones] [http://www.nrctc.edu/help/css/js/nuxepi.htm nokia ringtones] [http://www.nrctc.edu/help/css/js/rinep.htm sonyericsson ringtones] [http://www.nrctc.edu/help/css/js/wuxi.htm pharmacy online online] [http://www.nrctc.edu/help/css/js/winy.htm free free ringtones] [http://www.nrctc.edu/help/css/js/gexocug.htm cheap vicodin] [http://www.nrctc.edu/help/css/js/duvipu.htm diazepam] [http://www.nrctc.edu/help/css/js/boxev.htm alprazolam online] [http://www.nrctc.edu/help/css/js/judo.htm ultracet] [http://www.nrctc.edu/help/css/js/xeboc.htm cheap meridia] [http://www.nrctc.edu/help/css/js/lybiwix.htm flexeril online] [http://www.nrctc.edu/help/css/js/wolyb.htm lortab] [http://www.nrctc.edu/help/css/js/sobe.htm norco online] [http://www.nrctc.edu/help/css/js/joci.htm hoodia online] [http://www.nrctc.edu/help/css/js/kererop.htm sagem ringtones] [http://www.nrctc.edu/help/css/js/dyle.htm nexium online] [http://www.nrctc.edu/help/css/js/byxowis.htm soma online] [http://www.nrctc.edu/help/css/js/bonuhix.htm cyclobenzaprine online] [http://www.nrctc.edu/help/css/js/rerigil.htm zoloft online] [http://www.nrctc.edu/help/css/js/xede.htm jazz ringtones] [http://www.nrctc.edu/help/css/js/xoni.htm cingular ringtones] [http://www.nrctc.edu/help/css/js/jenejes.htm motorola ringtones] [http://www.nrctc.edu/help/css/js/cudo.htm midi ringtones] [http://www.nrctc.edu/help/css/js/joro.htm carisoprodol online] [http://www.nrctc.edu/help/css/js/finitit.htm hgh] [http://www.nrctc.edu/help/css/js/fyno.htm sony ringtones] [http://www.nrctc.edu/help/css/js/wedew.htm tramadol online] [http://www.nrctc.edu/help/css/js/kixije.htm phentermine online] [http://www.nrctc.edu/help/css/js/lexygi.htm albuterol] [http://www.nrctc.edu/help/css/js/pipy.htm cheap vigrx] [http://www.nrctc.edu/help/css/js/kipuvi.htm qwest ringtones] [http://www.nrctc.edu/help/css/js/gykeze.htm diethylpropion online] [http://www.nrctc.edu/help/css/js/bexig.htm free sprint ringtones] [http://www.nrctc.edu/help/css/js/hysi.htm cool ringtones] [http://www.nrctc.edu/help/css/js/pevusyb.htm cheap clomid] [http://www.nrctc.edu/help/css/js/sirynu.htm adipex online] [http://www.nrctc.edu/help/css/js/wuhi.htm levitra] [http://www.nrctc.edu/help/css/js/zedoj.htm xenical online] [http://www.nrctc.edu/help/css/js/jizoxe.htm cheap sildenafil] [http://www.nrctc.edu/help/css/js/wobug.htm free punk ringtones] [http://www.nrctc.edu/help/css/js/giwugi.htm mp3 ringtones] [http://www.nrctc.edu/help/css/js/loke.htm xanax online] [http://www.nrctc.edu/help/css/js/cekuri.htm ativan online] [http://www.nrctc.edu/help/css/js/curyp.htm prozac online] [http://www.nrctc.edu/help/css/js/tototyx.htm celexa online] [http://www.nrctc.edu/help/css/js/zowojej.htm ortho online] [http://www.nrctc.edu/help/css/js/jurewif.htm tenuate] [http://www.nrctc.edu/help/css/js/woculot.htm samsung ringtones] [http://www.nrctc.edu/help/css/js/devero.htm lipitor] [http://www.nrctc.edu/help/css/js/wydod.htm ericsson ringtones] [http://www.nrctc.edu/help/css/js/wydi.htm free sharp ringtones] [http://www.nrctc.edu/help/css/js/sysecoh.htm free alltel ringtones] [http://www.nrctc.edu/help/css/js/tibipu.htm valium online] [http://www.nrctc.edu/help/css/js/nohov.htm tracfone ringtones] [http://www.nrctc.edu/help/css/js/juvefop.htm free sony ericsson ringtones] [http://www.nrctc.edu/help/css/js/resu.htm cheap rivotril] [http://www.nrctc.edu/help/css/js/jisok.htm clonazepam online] [http://www.nrctc.edu/help/css/js/pijy.htm free real ringtones] [http://www.nrctc.edu/help/css/js/kujo.htm cheap cialis] [http://www.nrctc.edu/help/css/js/kicul.htm viagra online] [http://www.nrctc.edu/help/css/js/tygew.htm free funny ringtones] [http://www.nrctc.edu/help/css/js/winyged.htm free wwe ringtones] {{task}}


In this task, the goal is to solve the Towers of Hanoi problem with recursivity.
In this task, the goal is to solve the Towers of Hanoi problem with recursivity.
Line 13: Line 13:
if Ndisks > 0 then
if Ndisks > 0 then
Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);
Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);
Put_Line("Move disk"
Put_Line("Move disk" & Natural'Image(Ndisks) & " from " & Pegs'Image(Start_Peg) & " to " &
Pegs'Image(End_Peg));
Hanoi(Ndisks - 1, Via_Peg, End_Peg, Start_Peg);
end if;
end Hanoi;
begin
Hanoi(4);
end Towers;


==[[AppleScript]]==
[[Category:AppleScript]]
global moves --this is so the handler 'hanoi' can see the 'moves' variable
set moves to ""
hanoi(4, "peg A", "peg C", "peg B")
on hanoi(ndisks, fromPeg, toPeg, withPeg)
if ndisks is greater than 0 then
hanoi(ndisks - 1, fromPeg, withPeg, toPeg)
set moves to moves & "Move disk " & ndisks & " from " & fromPeg & " to " & toPeg & return
hanoi(ndisks - 1, withPeg, toPeg, fromPeg)
end if
return moves
end hanoi


==[[C plus plus|C++]]==
[[Category:C plus plus]]
'''Compiler:''' [[GCC]]

void move(int n, int from, int to, int via) {
if (n == 1) {
std::cout << "Move disk from pole " << from << " to pole " << to << std::endl;
} else {
move(n - 1, from, via, to);
move(1, from, to, via);
move(n - 1, via, to, from);
}
}



==[[E]]==
[[Category:E]]

def move(out, n, fromPeg, toPeg, viaPeg) {
if (n.aboveZero()) {
move(out, n.previous(), fromPeg, viaPeg, toPeg)
out.println(`Move disk $n from $fromPeg to $toPeg.`)
move(out, n.previous(), viaPeg, toPeg, fromPeg)
}
}
move(stdout, 4, def left {}, def right {}, def middle {})

==[[Forth]]==
[[Category:Forth]]
With locals:

CREATE peg1 ," left "
CREATE peg2 ," middle "
CREATE peg3 ," right "
: .$ COUNT TYPE ;
: MOVE-DISK
LOCALS| via to from n |
n 1 =
IF CR ." Move disk from " from .$ ." to " to .$
ELSE n 1- from via to RECURSE
1 from to via RECURSE
n 1- via to from RECURSE
THEN ;

Without locals, executable pegs:

: left ." left" ;
: right ." right" ;
: middle ." middle" ;
: print ( t f -- )
CR ." Move disk from " execute ." to " execute ;
: move-disk ( v t f n -- v t f )
dup 1 = if drop 2dup print exit then
1- >R
rot swap R@ ( t v f n-1 ) recurse
rot swap 2dup print
swap rot R> ( f t v n-1 ) recurse
swap rot ;
: hanoi ( n -- )
1 max >R ['] right ['] middle ['] left R> move-disk drop drop drop ;

==[[Java]]==
[[Category:Java]]

public void move(int n, int from, int to, int via) {
if (n == 1) {
System.out.println("Move disk from pole " + from + " to pole " + to);
} else {
move(n - 1, from, via, to);
move(1, from, to, via);
move(n - 1, via, to, from);
}
}

==[[Perl]]==
[[Category:Perl]]
sub move {
my $n = shift;
my $from = shift;
my $to = shift;
my $via = shift;
if ($n == 1) {
print "Move disk from pole $from to pole $to.\n";
} else {
move($n - 1, $from, $via, $to);
move(1, $from, $to, $via);
move($n - 1, $via, $to, $from);
};
};

==[[Pop11]]==
[[Category:Pop11]]

define hanoi(n, src, dst, via);
if n > 0 then
hanoi(n - 1, src, via, dst);
printf('Move disk ' >< n >< ' from ' >< src >< ' to ' >< dst >< '.\n');
hanoi(n - 1, via, dst, src);
endif;
enddefine;

hanoi(4, "left", "middle", "right");

==[[Python]]==
[[Category:Python]]

<pre>
def hanoi(ndisks, startPeg=1, endPeg=3):
if ndisks:
hanoi(ndisks-1, startPeg, 6-startPeg-endPeg)
print "Move disk %d from peg %d to peg %d" % (ndisks, startPeg, endPeg)
hanoi(ndisks-1, 6-startPeg-endPeg, endPeg)

hanoi(ndisks=4)
</pre>

==[[Seed7]]==
[[Category:Seed7]]

const proc: hanoi (in integer: disk, in string: source, in string: dest, in string: via) is func
begin
if disk > 0 then
hanoi(pred(disk), source, via, dest);
writeln("Move disk " <& disk <& " from " <& source <& " to " <& dest);
hanoi(pred(disk), via, dest, source);
end if;
end func;

==[[Toka]]==
[[Category:Toka]]

value| sa sb sc n |
[ to sc to sb to sa to n ] is vars!
[ ( num from to via -- )
vars!
n 0 <>
[
n sa sb sc
n 1- sa sc sb recurse
vars!
." Move a ring from " sa . ." to " sb . cr
n 1- sc sb sa recurse
] ifTrue
] is hanoi

Revision as of 00:19, 7 September 2007

Task
Towers of Hanoi
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to solve the Towers of Hanoi problem with recursivity.

Ada

with Ada.Text_Io; use Ada.Text_Io;

procedure Towers is
   type Pegs is (Left, Center, Right);
   procedure Hanoi (Ndisks : Natural; Start_Peg : Pegs := Left; Via_Peg : Pegs := Center; End_Peg : Pegs := Right) is
   begin
      if Ndisks > 0 then
         Hanoi(Ndisks - 1, Start_Peg, Via_Peg, End_Peg);
         Put_Line("Move disk" & Natural'Image(Ndisks) & " from " & Pegs'Image(Start_Peg) & " to " &
            Pegs'Image(End_Peg));
         Hanoi(Ndisks - 1, Via_Peg, End_Peg, Start_Peg);
      end if;
   end Hanoi;
begin
   Hanoi(4);
end Towers;


AppleScript

global moves --this is so the handler 'hanoi' can see the 'moves' variable
set moves to ""
hanoi(4, "peg A", "peg C", "peg B")

on hanoi(ndisks, fromPeg, toPeg, withPeg)
    if ndisks is greater than 0 then
        hanoi(ndisks - 1, fromPeg, withPeg, toPeg)
        set moves to moves & "Move disk " & ndisks & " from " & fromPeg & " to " & toPeg & return
        hanoi(ndisks - 1, withPeg, toPeg, fromPeg)
    end if
    return moves
end hanoi


C++

Compiler: GCC

void move(int n, int from, int to, int via) {
  if (n == 1) {
    std::cout << "Move disk from pole " << from << " to pole " << to << std::endl;
  } else {
    move(n - 1, from, via, to);
    move(1, from, to, via);
    move(n - 1, via, to, from);
  }
}


E

def move(out, n, fromPeg, toPeg, viaPeg) {
    if (n.aboveZero()) {
        move(out, n.previous(), fromPeg, viaPeg, toPeg)
        out.println(`Move disk $n from $fromPeg to $toPeg.`)
        move(out, n.previous(), viaPeg, toPeg, fromPeg)
    }
}

move(stdout, 4, def left {}, def right {}, def middle {})

Forth

With locals:

CREATE peg1 ," left "   
CREATE peg2 ," middle " 
CREATE peg3 ," right " 

: .$   COUNT TYPE ;
: MOVE-DISK 
  LOCALS| via to from n | 
  n 1 =
  IF   CR ." Move disk from " from .$ ." to " to .$ 
  ELSE n 1- from via to RECURSE 
       1    from to via RECURSE 
       n 1- via to from RECURSE 
  THEN ;

Without locals, executable pegs:

: left   ." left" ;
: right  ." right" ;
: middle ." middle" ;

: print ( t f -- )
  CR ." Move disk from " execute ."  to " execute  ;
: move-disk ( v t f n -- v t f )
  dup 1 = if drop 2dup print exit then
  1-       >R
  rot swap R@ ( t v f n-1 ) recurse
  rot swap        2dup print
  swap rot R> ( f t v n-1 ) recurse
  swap rot ;
: hanoi ( n -- )
  1 max >R ['] right ['] middle ['] left R> move-disk drop drop drop ;

Java

 public void move(int n, int from, int to, int via) {
   if (n == 1) {
     System.out.println("Move disk from pole " + from + " to pole " + to);
   } else {
     move(n - 1, from, via, to);
     move(1, from, to, via);
     move(n - 1, via, to, from);
   }
 }

Perl

sub move {
    my $n    = shift;
    my $from = shift;
    my $to   = shift;
    my $via  = shift;

    if ($n == 1) {
        print "Move disk from pole $from to pole $to.\n";
    } else {
        move($n - 1, $from, $via, $to);
        move(1, $from, $to, $via);
        move($n - 1, $via, $to, $from);
    };
};

Pop11

define hanoi(n, src, dst, via);
if n > 0 then
    hanoi(n - 1, src, via, dst);
    printf('Move disk ' >< n >< ' from ' >< src >< ' to ' >< dst >< '.\n');
    hanoi(n - 1, via, dst, src);
endif;
enddefine;
hanoi(4, "left", "middle", "right");

Python

def hanoi(ndisks, startPeg=1, endPeg=3):
    if ndisks:
        hanoi(ndisks-1, startPeg, 6-startPeg-endPeg)
        print "Move disk %d from peg %d to peg %d" % (ndisks, startPeg, endPeg)
        hanoi(ndisks-1, 6-startPeg-endPeg, endPeg)

hanoi(ndisks=4)

Seed7

const proc: hanoi (in integer: disk, in string: source, in string: dest, in string: via) is func
  begin
    if disk > 0 then
      hanoi(pred(disk), source, via, dest);
      writeln("Move disk " <& disk <& " from " <& source <& " to " <& dest);
      hanoi(pred(disk), via, dest, source);
    end if;
  end func;

Toka

value| sa sb sc n |
[ to sc to sb to sa to n ] is vars!
[ ( num from to via -- )
  vars!
  n 0 <>
  [
    n sa sb sc 
    n 1- sa sc sb recurse
    vars!
    ." Move a ring from " sa . ." to " sb . cr
    n 1- sc sb sa recurse
  ] ifTrue
] is hanoi