Jump to content

Towers of Hanoi: Difference between revisions

Added COBOL solution. Corrected GOSUB BASIC example, PL/I and R lang tags. Moved Emacs Lisp example to correct position. Re-formatted BASIC and BASIC256 sections.
m (standard BASIC vs. "old" BASIC)
(Added COBOL solution. Corrected GOSUB BASIC example, PL/I and R lang tags. Moved Emacs Lisp example to correct position. Re-formatted BASIC and BASIC256 sections.)
Line 130:
 
=={{header|BASIC}}==
===Using a Subroutine===
{{works with|FreeBASIC}}
{{works with|RapidQ}}
<lang freebasic>SUB move (n AS Integer, fromPeg AS Integer, toPeg AS Integer, viaPeg AS Integer)
IF n>0 THEN
Line 139 ⟶ 142:
 
move 4,1,2,3</lang>
{{works with|FreeBASIC}}
==={{header|RapidQ}}===
{{works with|RapidQ}}
 
===Using <code>GOSUB</code>s===
=={{header|BASIC}}==
Here's an example of implementing recursion in an old BASIC that only has global variables:
{{works with|Applesoft BASIC}}
<lang BASIC>10 DIM N(1024), F(1024), T(1024), V(1024): REM STACK PER PARAMETER
{{works with|Commodore BASIC}}
<lang BASICgwbasic>10 DIM N(1024), F(1024), T(1024), V(1024): REM STACK PER PARAMETER
20 SP = 0: REM STACK POINTER
30 N(SP) = 4: REM START WITH 4 DISCS
Line 171 ⟶ 173:
250 SP = SP - 1 : REM RESTORE STACK POINTER FOR CALLER
260 RETURN</lang>
==={{header|Applesoft BASIC}}===
{{works with|Applesoft BASIC}}
==={{header|Commodore BASIC}}===
{{works with|Commodore BASIC}}
 
=={{header|BASIC256}}==
Line 189 ⟶ 187:
end subroutine</lang>
 
{{out}}
 
<pre>
Output:<br>
Move disk from 1 to 3<br>
Move disk from 1 to 2<br>
Move disk from 3 to 2<br>
Move disk from 1 to 3<br>
Move disk from 2 to 1<br>
Move disk from 2 to 3<br>
Move disk from 1 to 3<br>
Move disk from 1 to 2<br>
Move disk from 3 to 2<br>
Move disk from 3 to 1<br>
Move disk from 2 to 1<br>
Move disk from 3 to 2<br>
Move disk from 1 to 3<br>
Move disk from 1 to 2<br>
Move disk from 3 to 2<br>
Towers of Hanoi puzzle completed!
</langpre>
 
=={{header|BBC BASIC}}==
Line 386 ⟶ 385:
(println (format "Move from %s to %s" from to))
(recur (dec n) via to from))))</lang>
 
==={{header|RapidQCOBOL}}===
{{trans|C}}
{{works with|OpenCOBOL|2.0}}
<lang cobol> >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. towers-of-hanoi.
 
PROCEDURE DIVISION.
CALL "move-disk" USING 4, 1, 2, 3
.
END PROGRAM towers-of-hanoi.
 
IDENTIFICATION DIVISION.
PROGRAM-ID. move-disk RECURSIVE.
 
DATA DIVISION.
LINKAGE SECTION.
01 n PIC 9 USAGE COMP.
01 from-pole PIC 9 USAGE COMP.
01 to-pole PIC 9 USAGE COMP.
01 via-pole PIC 9 USAGE COMP.
 
PROCEDURE DIVISION USING n, from-pole, to-pole, via-pole.
IF n > (t0
SUBTRACT 1 FROM n
CALL "move-disk" USING CONTENT n, from-pole, via-pole, to-pole
DISPLAY "Move disk from pole " from-pole " to pole " to-pole
CALL "move-disk" USING CONTENT n, via-pole, to-pole, from-pole
END-IF
.
END PROGRAM move-disk.</lang>
 
=={{header|CoffeeScript}}==
Line 405 ⟶ 436:
(format t "Move from ~A to ~A.~%" from to)
(move (- n 1) via to from))))</lang>
 
=={{header|Emacs Lisp}}==
{{Trans|Common Lisp}}
<lang lisp>
(defun move (n from to via)
(cond ((= n 1)
(print (format "Move from %S to %S" from to)))
(t
(progn
(move (- n 1) from via to)
(print (format "Move from %S to %S" from to))
(move (- n 1) via to from)))))
</lang>
 
=={{header|D}}==
Line 654 ⟶ 672:
 
move(stdout, 4, def left {}, def right {}, def middle {})</lang>
 
=={{header|BASICEmacs Lisp}}==
{{Trans|Common Lisp}}
<lang lisp>
(defun move (n from to via)
(cond ((= n 1)
(print (format "Move from %S to %S" from to)))
(t
(progn
(move (- n 1) from via to)
(print (format "Move from %S to %S" from to))
(move (- n 1) via to from)))))
</lang>
 
=={{header|Erlang}}==
Line 1,596 ⟶ 1,627:
=={{header|PL/I}}==
{{trans|Fortran}}
<lang PL/Ipli>tower: proc options (main);
 
call Move (4,1,2,3);
Line 1,728 ⟶ 1,759:
=={{header|R}}==
{{trans|Octave}}
<lang Rrsplus>hanoimove <- function(ndisks, from, to, via) {
if ( ndisks == 1 )
cat("move disk from", from, "to", to, "\n")
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.