Lambda Calculus Hamming Numbers

LA home
   Y (lazy)
   Y (strict)
   Thue seqs.
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),

 first = lambda n. lambda l.
   if n <= 0 then nil else hd l :: first (n-1) tl l

in let rec
 hamm = 1 :: (merge (mul 2 hamm)
             (merge (mul 3 hamm)
                    (mul 5 hamm)))

in first 10 hamm

{\fB Hamming Numbers. \fP}

Note that 'hamm' is an infinite, self-referential list.


© L. Allison   (or as otherwise indicated),
Created with "vi (Linux or Solaris)",  charset=iso-8859-1,  fetched Thursday, 19-Apr-2018 05:53:32 EDT.

free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop,
Firefox web-browser, FlashBlock flash on/off.