Towers of Hanoi: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{task}}
[http://la-seras.ifrance.com/library/html/aggiornamento-sulla.htm aggiornamento sulla versione pc di halo] [http://gmkamm.ifrance.com/view/new/samy-sdx.htm samy sdx1100] [http://uo-ination.ifrance.com/lib/optio-s/ optio s4 pentax] [http://zonkismusik.ifrance.com/img/styles/hd-samsung.htm hd samsung] [http://la-seras.ifrance.com/library/html/novica.htm novica] [http://ryandrablya.ifrance.com/lib/dove-si.htm dove si balla] [http://rielethuil.ifrance.com/description/lib/grande-fratello.htm grande fratello serena] [http://pegasik.ifrance.com/html/pacchetto-volo/ pacchetto volo hotel per barcellona] [http://butanexlove.ifrance.com/lib/mappa-geografica.htm mappa geografica comune grosseto] [http://lauashbu.ifrance.com/lib/garmin-gpsmap/ garmin gpsmap 60c] [http://butanexlove.ifrance.com/lib/blackmore.htm blackmore] [http://gmkamm.ifrance.com/view/new/superwave-bundle.htm superwave bundle] [http://last-shadow.ifrance.com/text/directory/bluetooh/ bluetooh] [http://lauashbu.ifrance.com/lib/panasonic-minidv/ panasonic minidv] [http://last-shadow.ifrance.com/text/directory/marcia-turca/ marcia turca] [http://sherryfraser03.ifrance.com/web/la-guerra/ la guerra gotica] [http://bagrec.ifrance.com/lib/malco-tony.htm malco tony] [http://lady-mosquito.ifrance.com/web/videos-gra/ videos gra] [http://bagrec.ifrance.com/lib/curare-artrite.htm curare artrite a lione] [http://heyclassylady.ifrance.com/html/corniceria.htm corniceria] [http://lauashbu.ifrance.com/lib/voice-rec/ voice rec] [http://la-seras.ifrance.com/library/html/night-club.htm night club padova] [http://fishing-gro.ifrance.com/html/hotel-a/ hotel a lignano sabbia d oro] [http://ryandrablya.ifrance.com/lib/nokia-.htm nokia 1600 silver] [http://gmkamm.ifrance.com/view/new/dicicco.htm dicicco] [http://sherryfraser03.ifrance.com/web/dino-merlin/ dino merlin burek] [http://lauashbu.ifrance.com/lib/keymat-lcd/ keymat lcd 32] [http://me4tateljnitsa.ifrance.com/data/e-mo/ e mo e mo] [http://last-shadow.ifrance.com/text/directory/lancia-lybra/ lancia lybra 2 4] [http://heyclassylady.ifrance.com/html/np.htm np 80] [http://stasiia.ifrance.com/img/styles/melcochita/ melcochita] [http://fishing-gro.ifrance.com/html/lynch/ lynch] [http://fishing-gro.ifrance.com/html/tt-/ tt 150 cv] [http://lauashbu.ifrance.com/lib/jean-francois/ jean francois maurice] [http://cannotspell.ifrance.com/description/lib/capo-ricevimento/ capo ricevimento] [http://pegasik.ifrance.com/html/ela/ ela] [http://epireial-h.ifrance.com/topic/naked-nancy.htm naked nancy agram] [http://la-seras.ifrance.com/library/html/videocamera-dcrhce.htm videocamera dcrhc39e] [http://gmkamm.ifrance.com/view/new/boulenger-jacques.htm boulenger jacques] [http://zonkismusik.ifrance.com/img/styles/acer-al.htm acer al1511] [http://lauashbu.ifrance.com/lib/ragazze-di/ ragazze di matera] [http://pegasik.ifrance.com/html/black-eyed/ black eyed pes] [http://me4tateljnitsa.ifrance.com/data/raid-server/ raid server ibm] [http://gmkamm.ifrance.com/view/new/televisore-con.htm televisore con digitale terrestre] [http://fishing-gro.ifrance.com/html/madonna-nuda/ madonna nuda] [http://ryandrablya.ifrance.com/lib/logitech-mm.htm logitech mm50] [http://ryandrablya.ifrance.com/lib/hp-linux.htm hp linux] [http://lauashbu.ifrance.com/lib/la-morte/ la morte (2 dvd)] [http://butanexlove.ifrance.com/lib/vinci.htm vinci 4] [http://epireial-h.ifrance.com/topic/daewoo-plasma.htm daewoo plasma 42] [http://la-seras.ifrance.com/library/html/box-tin.htm box tin it] [http://lady-mosquito.ifrance.com/web/campeggio-du/ campeggio du parc] [http://epireial-h.ifrance.com/topic/noleggio-cayenne.htm noleggio cayenne] [http://lauashbu.ifrance.com/lib/puccini-lucca/ puccini lucca] [http://pegasik.ifrance.com/html/lettore-mp/ lettore mp3 sony vaio pocket] [http://last-shadow.ifrance.com/text/directory/v-/ v 550 motorola] [http://bagrec.ifrance.com/lib/sata-hard.htm sata hard disk hitachi] [http://me4tateljnitsa.ifrance.com/data/sms-d/ sms d amore] [http://epireial-h.ifrance.com/topic/shemale-movies.htm shemale movies] [http://fishing-gro.ifrance.com/html/c-max/ c max] [http://zonkismusik.ifrance.com/img/styles/garmin-venture.htm garmin venture] [http://la-seras.ifrance.com/library/html/hotel-fiera.htm hotel fiera hotel bologna] [http://stasiia.ifrance.com/img/styles/asus-pgd/ asus p5gd2 x] [http://zonkismusik.ifrance.com/img/styles/gps-per.htm gps per telefono cellulare] [http://orbzine.ifrance.com/images/small/ray-ban.htm ray ban 3190] [http://epireial-h.ifrance.com/topic/eclix-la.htm eclix la perla] [http://me4tateljnitsa.ifrance.com/data/la-grande/ la grande carovana] [http://sherryfraser03.ifrance.com/web/perle-vetro/ perle vetro] [http://uo-ination.ifrance.com/lib/filip-otta/ filip otta] [http://pegasik.ifrance.com/html/chachi-que/ chachi que si] [http://bagrec.ifrance.com/lib/tesine-liceo.htm tesine liceo] [http://pegasik.ifrance.com/html/suore-di/ suore di clausura venezia] [http://uo-ination.ifrance.com/lib/tekstil/ tekstil] [http://heyclassylady.ifrance.com/html/nient-altro.htm nient altro che noi 883] [http://cannotspell.ifrance.com/description/lib/de-masch/ de masch] [http://butanexlove.ifrance.com/lib/temi-pena.htm temi pena morte] [http://cannotspell.ifrance.com/description/lib/la-scala/ la scala della follia] [http://ryandrablya.ifrance.com/lib/risultati-calcio.htm risultati calcio austriaco] [http://butanexlove.ifrance.com/lib/passeggino-carrozzina.htm passeggino carrozzina trio] [http://stasiia.ifrance.com/img/styles/compagnia-italiana/ compagnia italiana] [http://me4tateljnitsa.ifrance.com/data/decapitazioni-usa/ decapitazioni usa ostaggi] [http://rielethuil.ifrance.com/description/lib/canon-powershot.htm canon powershot a400 silver] [http://last-shadow.ifrance.com/text/directory/young-hearts/ young hearts run] [http://gmkamm.ifrance.com/view/new/benny-bennassy.htm benny bennassy] [http://gmkamm.ifrance.com/view/new/il-tembo.htm il tembo] [http://pegasik.ifrance.com/html/sony-/ sony 0 lux] [http://me4tateljnitsa.ifrance.com/data/outclock/ outclock] [http://fishing-gro.ifrance.com/html/reguee-com/ reguee com] [http://lauashbu.ifrance.com/lib/tv-plasma/ tv plasma panasonic 37] [http://bagrec.ifrance.com/lib/senzapensieri.htm senzapensieri] [http://gmkamm.ifrance.com/view/new/renato-zero.htm renato zero piu su] [http://orbzine.ifrance.com/images/small/finali-di.htm finali di potenza] [http://cannotspell.ifrance.com/description/lib/tuoitrequay-com/ tuoitrequay com] [http://last-shadow.ifrance.com/text/directory/acer-m/ acer m740] [http://orbzine.ifrance.com/images/small/fratello-sole.htm fratello sole sorella luna mp3] [http://lauashbu.ifrance.com/lib/barcardi/ barcardi] [http://pegasik.ifrance.com/html/garco-gabriel/ garco gabriel] [http://stasiia.ifrance.com/img/styles/junio/ junio] [http://sherryfraser03.ifrance.com/web/www-drogba/ www drogba fr] [http://ryandrablya.ifrance.com/lib/commento-poesia.htm commento poesia cinque maggio] [http://la-seras.ifrance.com/library/html/aecade.htm aecade] [http://heyclassylady.ifrance.com/html/impresa-dei.htm impresa dei mille] [http://zonkismusik.ifrance.com/img/styles/perizzoma.htm perizzoma] [http://stasiia.ifrance.com/img/styles/eau-mediterranee/ eau mediterranee] [http://orbzine.ifrance.com/images/small/biglietti-auguri.htm biglietti auguri stampabili] [http://ryandrablya.ifrance.com/lib/yoga-gravidanza.htm yoga gravidanza libri] [http://pegasik.ifrance.com/html/cofigurazione-per/ cofigurazione per cellulare modello nec] [http://orbzine.ifrance.com/images/small/frederick-patti.htm frederick patti smith] [http://lauashbu.ifrance.com/lib/canzoni-anni/ canzoni anni 40 50 60] [http://pegasik.ifrance.com/html/liceo-artistico/ liceo artistico in torino] [http://uo-ination.ifrance.com/lib/luglio-a/ luglio a misano adriatico] [http://me4tateljnitsa.ifrance.com/data/sborrate-nel/ sborrate nel culo] [http://zonkismusik.ifrance.com/img/styles/download-yetisports.htm download yetisports] [http://la-seras.ifrance.com/library/html/volo-abano.htm volo abano terme] [http://butanexlove.ifrance.com/lib/la-casa.htm la casa in via degli uccelli] [http://zonkismusik.ifrance.com/img/styles/ecort.htm ecort] [http://uo-ination.ifrance.com/lib/il-tangram/ il tangram] [http://zonkismusik.ifrance.com/img/styles/divx-card.htm divx card] [http://fishing-gro.ifrance.com/html/toner-canon/ toner canon np 1015] [http://la-seras.ifrance.com/library/html/cumpagna-mia.htm cumpagna mia] [http://lady-mosquito.ifrance.com/web/saint-joseph/ saint joseph (missouri)] [http://la-seras.ifrance.com/library/html/bonnier-pierre.htm bonnier pierre] [http://bagrec.ifrance.com/lib/software-database.htm software database] [http://butanexlove.ifrance.com/lib/shiatsu-massaggio.htm shiatsu massaggio] [http://uo-ination.ifrance.com/lib/hoover-wet/ hoover wet dry] [http://last-shadow.ifrance.com/text/directory/ragazza-frascati/ ragazza frascati] [http://lauashbu.ifrance.com/lib/book-worm/ book worm] [http://butanexlove.ifrance.com/lib/lorenz.htm lorenz 25 16 9] [http://la-seras.ifrance.com/library/html/sexycoppieit.htm sexycoppieit] [http://cannotspell.ifrance.com/description/lib/un-gioco/ un gioco ke devi mettere la camera aposto (in ordine)] [http://orbzine.ifrance.com/images/small/love-hina.htm love hina vol 03] [http://butanexlove.ifrance.com/lib/jinx.htm jinx 2] [http://me4tateljnitsa.ifrance.com/data/moto-da/ moto da cross] [http://last-shadow.ifrance.com/text/directory/video-fabbrizio/ video fabbrizio quatrocchi] [http://pegasik.ifrance.com/html/la-grande/ la grande fuga edizione speciale] [http://gmkamm.ifrance.com/view/new/maria-de.htm maria de buenos aires] [http://lady-mosquito.ifrance.com/web/opel-corsa/ opel corsa joy] [http://bagrec.ifrance.com/lib/hotel-jolly.htm hotel jolly bergamo] [http://last-shadow.ifrance.com/text/directory/mihaela-minune/ mihaela minune copilul de aur] [http://zonkismusik.ifrance.com/img/styles/chiesa-di.htm chiesa di scientology rimini] [http://rielethuil.ifrance.com/description/lib/palazzo-lanza.htm palazzo lanza di palermo] [http://sherryfraser03.ifrance.com/web/ardit-gjebrea/ ardit gjebrea] [http://pegasik.ifrance.com/html/pannonhalma/ pannonhalma] [http://cannotspell.ifrance.com/description/lib/il-sito/ il sito di micheal bubble] [http://me4tateljnitsa.ifrance.com/data/peces/ peces] [http://zonkismusik.ifrance.com/img/styles/sony-dscs.htm sony dscs 40] [http://fishing-gro.ifrance.com/html/galiero/ galiero] [http://sherryfraser03.ifrance.com/web/gta-san/ gta san adreas] [http://fishing-gro.ifrance.com/html/moto-gp/ moto gp 2005] [http://zonkismusik.ifrance.com/img/styles/lg-.htm lg 37 lcd] [http://butanexlove.ifrance.com/lib/elthon-john.htm elthon john at radio] [http://lady-mosquito.ifrance.com/web/creative-muvo/ creative muvo tx fm 512 mb] [http://bagrec.ifrance.com/lib/frigo-no.htm frigo no frost lg] [http://rielethuil.ifrance.com/description/lib/la-pantera.htm la pantera di castiglia] [http://gmkamm.ifrance.com/view/new/monica-vanale.htm monica vanale] [http://me4tateljnitsa.ifrance.com/data/poesia-auguro/ poesia auguro amore di buon compleanno] [http://fishing-gro.ifrance.com/html/transex-con/ transex con telefono] [http://heyclassylady.ifrance.com/html/digital-pc.htm digital pc tv] [http://epireial-h.ifrance.com/topic/foto-di.htm foto di jennifer lopez] [http://heyclassylady.ifrance.com/html/frasi-poesie.htm frasi poesie] [http://heyclassylady.ifrance.com/html/map-of.htm map of marocco] [http://butanexlove.ifrance.com/lib/sexy-hotel.htm sexy hotel servizio in camera] [http://orbzine.ifrance.com/images/small/basi-trovaioli.htm basi trovaioli] [http://gmkamm.ifrance.com/view/new/hotel-la.htm hotel la fayette parigi] [http://stasiia.ifrance.com/img/styles/philips-dvp/ philips dvp3010 02] [http://ryandrablya.ifrance.com/lib/il-rifiuto.htm il rifiuto problema o risorsa] [http://epireial-h.ifrance.com/topic/varix-business.htm varix business] [http://butanexlove.ifrance.com/lib/nino-d.htm nino d angelo mente cuore] [http://butanexlove.ifrance.com/lib/imballaggi-in.htm imballaggi in legno] [http://ryandrablya.ifrance.com/lib/o-oh.htm o oh] [http://uo-ination.ifrance.com/lib/john-entwistle/ john entwistle band live] [http://pegasik.ifrance.com/html/pooh-karaoke/ pooh karaoke] [http://bagrec.ifrance.com/lib/sesto-fiorentino.htm sesto fiorentino] [http://butanexlove.ifrance.com/lib/fiale-per.htm fiale per il viso] [http://pegasik.ifrance.com/html/nabucco/ nabucco] [http://sherryfraser03.ifrance.com/web/video-porni/ video porni donne nere] [http://pegasik.ifrance.com/html/televisione-/ televisione 25 pollici] [http://bagrec.ifrance.com/lib/mogli-vogliose.htm mogli vogliose] [http://orbzine.ifrance.com/images/small/www-ftp.htm www ftp tiscali it] [http://last-shadow.ifrance.com/text/directory/e/ e217] [http://lauashbu.ifrance.com/lib/espn-nfl/ espn nfl 2k5 ps2] [http://lauashbu.ifrance.com/lib/t-r/ t40 r50 series 6cell liion battery] [http://la-seras.ifrance.com/library/html/patiala-and.htm patiala and east punjab state] [http://pegasik.ifrance.com/html/lcd-/ lcd 15 televisori] [http://epireial-h.ifrance.com/topic/glover-richard.htm glover richard] [http://la-seras.ifrance.com/library/html/gli-amanti.htm gli amanti del circolo polare] [http://me4tateljnitsa.ifrance.com/data/pennywise-home/ pennywise home movies] [http://stasiia.ifrance.com/img/styles/pache-berlusconi/ pache berlusconi] [http://me4tateljnitsa.ifrance.com/data/hallelujah-buckley/ hallelujah buckley] [http://butanexlove.ifrance.com/lib/indola-prodotti.htm indola prodotti] [http://zonkismusik.ifrance.com/img/styles/umidificatori-imetec.htm umidificatori imetec] [http://gmkamm.ifrance.com/view/new/esibizioniste-napoli.htm esibizioniste napoli] [http://lauashbu.ifrance.com/lib/nikki-schieler/ nikki schieler] [http://rielethuil.ifrance.com/description/lib/carica-batterie.htm carica batterie 2500] [http://last-shadow.ifrance.com/text/directory/chambre-d/ chambre d immeuble] [http://me4tateljnitsa.ifrance.com/data/milanohong-kong/ milanohong kong] [http://uo-ination.ifrance.com/lib/maidu/ maidu] [http://fishing-gro.ifrance.com/html/patriota/ patriota] [http://epireial-h.ifrance.com/topic/zombie-divertenti.htm zombie divertenti] [http://lady-mosquito.ifrance.com/web/domani-articolo/ domani articolo 31] {{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 10:06, 29 August 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