Haskell homework

Thue sequence

Write a Haskell program to produce the infinite Thue sequence, t, defined by the limit

    lim i->infinity: ti
where
    t0 = 0
    ti+1 = ti [0->01, 1->10]

The last expression means replace (simultaenously) each 0 in ti by 01 and each 1 by 10. Thus

    t = 01101001100101101001011001101001...

Some properties that may be verified:

  1. ti+1 = ti ~ti , the catenation of ti and its complement.
  2. t = t # ~t, where # is the interleave operator defined on equal-length sequences:
        infixl 6 #    -- 6 is the same precedence as +
        (#) :: [a] -> [a] -> [a]
        [] # [] = []
        (x:xs) # (y:ys) = x : y : xs#ys
    
  3. The nth character of t (counted from 0) is the parity bit for the binary representation of n.
  4. t contains no `cubes', substrings of the form www, where w is nonempty.
  5. t2i is a palindrome.
Your program should produce the first n characters of t in time O(n), and notionally run forever. Note that naive counting of bits per property 2 takes time O(n logn).

Nand expressions

It is well known that nand is universal, in the sense that all Boolean functions are expressible in terms of nand. For the record, nand may be defined thus:
    nand :: Bool -> Bool -> Bool
    nand True True = False
    nand _ _ = True

Write a Haskell program that finds a minimal nand-only expression for each of the 16 binary operations with signature Bool×Bool->Bool. For example, the binary operation f(x,y) whose truth table is
xyf(x,y)
False False True
False True True
True False False
True True False
is equivalent to nand x x.

Problems designed by Doug McIlroy

Last modified:
Tue May 20 12:03:50 EDT 2003