The sequence begins 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... . Note that 7, 11, 13, 14 are not in the list.i j k 2 × 3 × 5, i,j,k>=0
The Hamming program (below) defines a list which begins with the first Hamming number `1':
If you take a Hamming number and multiply it by 2, 3 or 5 you get another Hamming number. A moment's thought reveals that all Hamming numbers, except 1, can be obtained in this way. The program therefore continues by merging the results of multiplying the members of the list by 2, 3 and 5.hamm = 1 :: ...
The merge and multiply functions are quite conventional. The program below never terminates but continues to print Hamming numbers until it runs out of space or is killed. If some finite part of the list were printed then such a program would terminate normally.merge (mult 2 hamm) (merge .... )
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}
Exercise: Modify the program above so that it prints only the first 20 Hamming numbers and then stops.