Anonymous user
Towers of Hanoi: Difference between revisions
m
Fixed lang tags.
Underscore (talk | contribs) (Perl 5: Changed to permit calling with one argument. Perl 6: Added.) |
m (Fixed lang tags.) |
||
Line 2:
=={{header|ActionScript}}==
<lang actionscript>public function move(n:int, from:int, to:int, via:int):void
{
if (n > 0)
Line 11 ⟶ 10:
move(n - 1, via, to, from);
}
}</lang>
=={{header|Ada}}==
<lang 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; End_Peg : Pegs := Right; Via_Peg : Pegs := Center) is
begin
if Ndisks > 0 then
Put_Line("Move disk" &
Hanoi(Ndisks - 1, Via_Peg,
end if;
begin
Hanoi(4);
end Towers;</lang>
=={{header|ALGOL 68}}==
)</lang>
=={{header|AmigaE}}==
Line 61 ⟶ 57:
=={{header|AppleScript}}==
<lang applescript>
=={{header|AutoHotkey}}==
<lang AutoHotkey>move(n, from, to, via) ;n = # of disks, from = start pole, to = end pole, via = remaining pole
{
if (n = 1)
{
msgbox , Move disk from pole %from% to pole %to%
}
move(n-1, from, via, to)
}
}</lang>
=={{header|AWK}}==
{{trans|Logo}}
<lang AWK>$ awk 'func hanoi(n,f,t,v){if(n>0){hanoi(n-1,f,v,t);print(f,"->",t);hanoi(n-1,v,t,f)}}
BEGIN{hanoi(4,"left","middle","right")}'
left -> right
Line 110 ⟶ 103:
left -> right
left -> middle
right -> middle</lang>
=={{header|BASIC}}==
Line 129 ⟶ 121:
=={{header|C}}==
<lang c>#include <stdio.h>
void move(int n, int from, int to, int via)
{
if (n > 0) {
}
int main()
{
move(4, 1,2,3);
return 0;
}</lang>
=={{header|C sharp|C#}}==
Line 160 ⟶ 150:
=={{header|C++}}==
{{works with|g++}}
<lang cpp>
=={{header|Clojure}}==
=={{header|Common Lisp}}==
<lang lisp>
=={{header|D}}==
Line 258 ⟶ 248:
]sM
[ # code block
ln # push n
lf # push from
Line 317 ⟶ 307:
=={{header|E}}==
<lang e>
=={{header|Erlang}}==
=={{header|FALSE}}==
=={{header|Forth}}==
With locals:
Without locals, executable pegs:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>
=={{header|Haskell}}==
Line 401 ⟶ 391:
Most of the programs on this page use an imperative approach (i.e., print out movements as side effects during program execution). Haskell favors a purely functional approach, where you would for example return a (lazy) list of movements from a to b via c:
<lang haskell>
One can use this function to produce output, just like the other programs:
<lang haskell>
Or, instead one can of course also program imperatively, using the IO monad directly:
<lang haskell>
=={{header|Io}}==
)</lang>
=={{header|J}}==
<lang j>H =: i.@(,&2) ` (({&0 2 1,0 2,{&1 0 2)@$:@<:) @. *
H1=: monad define
Line 441 ⟶ 430:
i.0 2
end.
)</lang>
<tt>H</tt> employs anonymous recursion; <tt>H1</tt> is an "explicit" statement of the same computation. For example:
<lang j> H 3
0 2
0 1
Line 452 ⟶ 439:
1 2
1 0
2 0</lang>
The result is a 2-column table; a row <tt>i,j</tt> is interpreted as: move a disk (the top disk) from peg <tt>i</tt> to peg<tt> j</tt> .
=={{header|Java}}==
<lang java>
=={{header|JavaScript}}==
<lang javascript>
=={{header|Joy}}==
From [http://www.latrobe.edu.au/philosophy/phimvt/joy/jp-nestrec.html here]
<lang joy>DEFINE hanoi == [[rolldown] infra] dip
[ [ [null] [pop pop] ]
[ [dup2 [[rotate] infra] dip pred]
Line 487 ⟶ 472:
[[swap] infra] dip pred ]
[] ] ]
condnestrec.</lang>
Using it (5 is the number of disks.)
<lang joy>[source destination temp] 5 hanoi.</lang>
=={{header|Logo}}==
=={{header|Mathematica}}==
=={{header|Modula-3}}==
Line 536 ⟶ 518:
The Interface - TowersOfHanoi.h:
<lang objc>#import <Foundation/NSObject.h>
@interface TowersOfHanoi: NSObject {
Line 548 ⟶ 529:
-(void) setPegFrom: (int) from andSetPegTo: (int) to andSetPegVia: (int) via andSetNumDisks: (int) disks;
-(void) movePegFrom: (int) from andMovePegTo: (int) to andMovePegVia: (int) via andWithNumDisks: (int) disks;
@end</lang>
The Implementation - TowersOfHanoi.m:
<lang objc>#import "TowersOfHanoi.h"
@implementation TowersOfHanoi
Line 574 ⟶ 553:
}
@end</lang>
Test code: TowersTest.m:
<lang objc>#import <stdio.h>
#import "TowersOfHanoi.h"
Line 598 ⟶ 575:
return 0;
}</lang>
=={{header|OCaml}}==
Line 633 ⟶ 609:
I think it is standard pascal, except for the constant array "strPole". I am not sure if constant arrays are part of the standard. However, as far as I know, they are a "de facto" standard in every compiler.
<lang pascal>
A little longer, but clearer for my taste
<lang pascal>
=={{header|Perl}}==
Line 706 ⟶ 682:
=={{header|PHP}}==
{{trans|Java}}
<lang php>function move($n,$from,$to,$via) {
if ($n === 1) {
print("Move disk from pole $from to pole $to");
Line 715 ⟶ 690:
move(n-1,$via,$to,From);
}
}</lang>
=={{header|Pop11}}==
=={{header|Python}}==
Line 792 ⟶ 766:
=={{header|Seed7}}==
=={{header|Tcl}}==
Line 837 ⟶ 811:
=={{header|Toka}}==
=={{header|UNIX Shell}}==
Line 855 ⟶ 829:
{{works with|bash}}
<lang bash>#!/bin/bash
move()
Line 875 ⟶ 848:
}
move $1 $2 $3 $4</lang>
=={{header|Ursala}}==
<lang Ursala>#import nat
move = ~&al^& ^rlPlrrPCT/~&arhthPX ^|W/~& ^|G/predecessor ^/~&htxPC ~&zyxPC
Line 886 ⟶ 857:
#show+
main = ^|T(~&,' -> '--)* move/4 <'start','end','middle'></lang>
output:
<pre>start -> middle
Line 907 ⟶ 877:
=={{header|Vedit macro language}}==
This implementation outputs the results in current edit buffer.
<lang vedit>#1=1; #2=2; #3=3; #4=4 // move 4 disks from 1 to 2
Call("MOVE_DISKS")
Return
Line 932 ⟶ 901:
Num_Pop(1,4)
}
Return</lang>
=={{header|Visual Basic .NET}}==
Line 949 ⟶ 917:
MoveTowerDisks(4, 1, 2, 3)
End Sub
End Module</lang>
=={{header|XSLT}}==
<xsl:call-template name="hanoi"><xsl:with-param name="n" select="4"/></xsl:call-template>
|