|
Calculate the Hamming numbers by the well-known recursive method;
also see the [PFL]
version.
let rec
merge = lambda a. lambda b.
if hd a < hd b then (hd a)::(merge tl a b)
else if hd b < hd a then (hd b)::(merge a tl b)
else (hd a)::(merge tl a tl b),
mul = lambda n. lambda l. (n* hd l)::(mul n tl l)
in let rec
hamm = 1 :: (merge (mul 2 hamm)
(merge (mul 3 hamm)
(mul 5 hamm)))
in hamm
{\fB Hamming Numbers. \fP}
|
The program, as it stands, will try to print the
infinitely many Hamming numbers
and will fall prey to arithmetic overflow and/or
will hit the limits of the execution constraints.
Modify it to print the first `n' Hamming numbers.
|
λ ...
:: | list cons |
nil | the [ ] list |
null | predicate |
hd | head (1st) |
tl | tail (rest) |
|
|
|