|| Miranda examples for 341 parameter passing lecture, Autumn 2005

|| An 'if' function in Miranda.  Since it uses lazy evaluation, the true
|| or false branches only get evaluated if its value is needed.
my_if True x y = x
my_if False x y = y

|| The infinite list of ones:
ones = 1 : ones

|| The infinite list of integers:
ints n = n : ints (n+1)


|| The infinite string "abcabcabc ..."
lets = "abc" ++ lets

|| Two prime number programs:

factors n = [k | k <- [1..n]; n mod k = 0]

dullprimes = filter isprime [2..]
             where
             isprime p = (factors p = [1,p])


interestingprimes = sieve [2..]
                    where
                    sieve (p:x) = p : sieve [n | n <- x; n mod p > 0]

|| Hamming numbers.  The Hamming numbers are all numbers of the form 
|| 2**i * 3**j * 5**k for all i,j,k >= 0.  The ham function should return
|| them in order, starting with 1.

my_merge (x:a) (y:b) = x : my_merge a (y:b) , if x<y
                     = y : my_merge a b , if x=y
                     = y : my_merge (x:a) b , otherwise

ham = 1: my_merge ham2 (my_merge ham3 ham5)

ham2 = map (*2) ham
ham3 = map (*3) ham
ham5 = map (*5) ham
