Towers of Hanoi: Difference between revisions

Content added Content deleted
({{out}})
Line 260:
& move$(4,1,2,3)
);</lang>
{{out}}
Output:
<pre>Move disk from pole 1 to pole 3
Move disk from pole 1 to pole 2
Line 1,000:
moveStack(STACK.A, STACK.C)</lang>
 
{{out}}
Output:
<pre style="height:30ex;overflow:scroll;">A: [( ), O, o, °]
B: []
Line 1,066:
 
=={{header|Haskell}}==
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:
(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>hanoi :: Integer -> a -> a -> a -> [(a, a)]
hanoi 0 _ _ _ = []
Line 1,282 ⟶ 1,284:
Called from command line:
<lang Lasso>./towers</lang>
{{out}}
Output:
<pre>Move disk from A to C
Move disk from A to B
Line 1,292 ⟶ 1,294:
Called from command line:
<lang Lasso>./towers 4</lang>
{{out}}
Output:
<pre>Move disk from A to B
Move disk from A to C
Line 1,399 ⟶ 1,401:
end</lang>
{{out|Sample output}}
<lang MATLABpre>towerOfHanoi(3,1,3,2)
Move plate 1 from tower 1 to tower 3
Move plate 2 from tower 1 to tower 2
Line 1,406 ⟶ 1,408:
Move plate 1 from tower 2 to tower 1
Move plate 2 from tower 2 to tower 3
Move plate 1 from tower 1 to tower 3</langpre>
 
=={{header|МК-61/52}}==
Line 1,943 ⟶ 1,945:
}
}</lang>
{{out}}
Output
<lang rascal>rascal>hanoi(4,1,3)
Move disk 1 from peg 1 to peg 2
Line 2,089 ⟶ 2,091:
The minimum number of moves to solve a 3 ring Tower of Hanoi is 7.
</pre>
'''output''' {{out}}when the following was entered (to solve with four disks): <tt>4</tt>
<pre style="height:25ex;overflow:scroll">
step 1: move disk 1 ──► 2
Line 2,504 ⟶ 2,506:
hanoi hanoi = eq(n,0) 1 :s(return)
hanoi(n - 1, src, tmp, trg)
count = count + 1
output = count ': Move disc from ' src ' to ' trg
hanoi(n - 1, tmp, trg, src) :(return)
Line 2,796 ⟶ 2,798:
MoveTower(3, "left", "right", "center")</lang>
 
{{out}}
Output:
<pre>
Move from left peg to right peg.