Alex Rivera | Logout

Infinite list of infinite counters

Asked 2012-01-06T19:30:38.770
10

For those with suspicious minds, this is not homework, just curious.

Given a finite alphabet, is it possible to construct a list of infinitely long words made from the alphabet in reverse lexographic order?

i.e. given the alphabet "ab"

is it possible to construct the list:

["aaaaaa...", "baaaaa...", "abaaaa...", "bbaaaa...", "aabaaa...", ...]

where ... represents the list (and list of lists) extending to infinite length.

A naïve attempt is:

counters alphabet = [c:ounter | ounter <- counters alphabet, c <- alphabet]

but this doesn't work since it is left recursive.

Of course, with a working version, if you tried to print the result, you would only see the first element being printed as an infinite list of the first element from the alphabet. However, you should be able to do this:

mapM_ (print . take 2) . take 4 . counters $ "ab"

and see the output:

aa
ba
ab
bb
Edit
Report

1 Answer

1

The progression looks like encoding a base-N number with the least-significant-digit on the left, so we could approach it as

  1. Make a "to base N" function f using your alphabets as the letters.
  2. map f to [0..]
  3. Append repeat $ head alphabets to each element of the list.
answered 2012-01-06T19:42:18.490

Your Answer