I need to generate an infinite list of strings using numbers and letters. The first string should simply be “a”, then “b” through to “z”, then “0” through to “9”, then “aa”, “ab” etc.
I can easily generate the ones with one character, but then it gets more complicated.
So, suppose we already had this list of all possible strings:
Given
allStrings, how could we create another list of all possible strings?Let’s look at each possible string as a single character prefix, and a string suffix
The string suffix is either the empty string or a member of the list of all possible strings.
The single character prefix is in
'a'thru'z'or'0'thru'9'.(this is using list comprehensions – we could also do the same using
concatMap:)
Now let’s go back to the original question. How do we find
allStrings?In most languages, we couldn’t – it’s an infinite list of strings, the program would never finish. But since Haskell is lazy, it’s cool with generating only as much of
allStringsas we actually use.One surprising thing that this lets us do is we can define
allStringsin terms ofalsoAllStrings.Or, more likely, in terms of itself.
This is called corecursive data – data that is defined in terms of itself.
Try it out in ghci:
The reason it works (and doesn’t just loop infinitely) is that it defines part of itself before it uses itself. The first elements we include in
allStringsare the single character extensions to the empty string"". So by the time we start iterating through the elements ofallStringsto use as suffixes, the first couple elementsallStringsare already defined and available. And the more suffixes we process, the more elements ofallStringsare defined and available to use as suffixes.It’s very similar to a common haskellism of defining the fibonacci numbers corecursively:
Don’t be surprised if corecursion takes a while to wrap your head around. It’s worth the effort to understand, though.