Lambda Calculus Hamming Numbers

LA home
Computing
FP
 λ-calc.
  Intro
  Syntax
  Examples
   Ints
   Bools
   Lists(1)
   arithmetic
   Y (lazy)
   Y (strict)
   Lists(2)
   Trees
   Primes(1)
   Fibonacci(1)
   Unique
   Hamming#s
   Composites
   Fibonacci(2)
   Thue seqs.
   Edit-dist.
   Hamming

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}

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.


The applet above needs Java on.
www

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

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Friday, 03-Sep-2010 13:40:04 EST.