Towers of Hanoi: Difference between revisions

→‎{{header|J}}: Add lang tags, improve readability of explicit defintion.
(→‎{{header|J}}: Add lang tags, improve readability of explicit defintion.)
Line 432:
 
=={{header|J}}==
<lang j>
H =: i.@(,&2) ` (({&0 2 1,0 2,{&1 0 2)@$:@<:) @. *
H1H =: 3 : 'ifi.@(,&2) *y do.` (({&0 2 1 , 0 2 , {&1 0 2)@$:@<:) H1 y-1 else@. i.0 2 end.'*
 
H1=: monad define
if. *y do.
H =: i.@(,&2) ` (({&0 2 1 , 0 2 , {&1 0 2)@$:@<:) @.H1 *y-1
else.
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
2 1
0 2
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> .
 
892

edits