20-CS-4003-001 Organization of Programming Languages Fall 2017
Recursion

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code

Example: Hamming sequence

 ```times::Num a => a -> [a] -> [a] times a [] = [] times a (b:y) = a*b : times a y merge::Ord a => [a] -> [a] -> [a] merge (a:x) [] = a:x merge (a:x) (b:y) = if (a < b) then a : merge x (b:y)) else b : merge (a:x) y ham::(Ord a, Num a) => [a] -> [a] ham [] = [] ham p = head p : merge (times (head p) (ham p)) (ham (tail p)) ``` - Given a list p of prime numbers, produce all numbers and only those numbers whose prime factors are contained in p. The list produced is infinitely large. Tokens of the list are actually not created until they are demanded. Thus, take 10 (ham [3,5,11]) will compute the first 10 tokens of the Hamming sequence for prime list 3,5,11 and the other tokens will not be computed. Observe that the merge function assumes the first of the two lists is never empty: that happens to be the case in this example.