factorial=: 1:`(]*factorial@<:) @. * factorial "0 i.6 1 1 2 6 24 120Note that 1: denotes the constant function whose result is 1 .
1:`(]*$:@<:) @. * "0 i. 6 1 1 2 6 24 120In the Tower of Hanoi puzzle, a set of n discs (each of a different size) is to be moved from post A to post B using a third post C and under the restriction that a larger disc is never to be placed on a smaller. The following is a recursive definition of the process:
h=: b`(p,.q,.r)@.c
c=: 1: < [
b=: 2&,@[ $ ]
p=: <:@[ h 1: A. ]
q=: 1: h ]
r=: <:@[ h 5: A. ]
3 h x=: 'ABC'
AABACCA
BCCBABB
0 1 2 3 4 <@h"0 1 x
++-+---+-------+---------------+
||A|AAC|AABACCA|AACABBAACCBCAAC|
||B|CBB|BCCBABB|CBBCACCBBAABCBB|
++-+---+-------+---------------+
Exercises
| 22.1 | Use the following as exercises in reading and writing:
f=:1:`(+//.@(,:~)@($:@<:))@.* Binomial Coeffs
<@f"0 i.6 Boxed binomials
g=:1:`((],+/@(_2&{.))@$:@<:)@.* Fibonacci
|