I’ve been reading Learn You a Haskell and got stuck on a tiny point at the beginning, in the section about ranges: you can’t have a range with more than one step value. This post is about what it might mean to allow multiple steps and how Charles Babbage might have had an opinion on this particular aspect of haskell’s design.

Here’s the whole paragraph from Learn You a Haskell,

*To make a list containing all the natural numbers from 1 to 20, you just write [1..20]. *[... snip ...]* **Ranges are cool because you can also specify a step. What if we want all even numbers between 1 and 20? Or every third number between 1 and 20?*

`ghci> [2,4..20] `

`[2,4,6,8,10,12,14,16,18,20] `

`ghci> [3,6..20] `

`[3,6,9,12,15,18]`

*It’s simply a matter of separating the first two elements with a comma and then specifying what the upper limit is. While pretty smart, ranges with steps aren’t as smart as some people expect them to be. You can’t do [1,2,4,8,16..100] and expect to get all the powers of 2. Firstly because you can only specify one step. And secondly because some sequences that aren’t arithmetic are ambiguous if given only by a few of their first terms.*

The bold highlight is mine. This makes sense – how can the language guess that what you have in mind when you write `[1, 2, 4, 8, 16..]`

is the powers of 2?

On the other hand though, just as a fun exercise, if you were to allow multiple step values how would you define them? Even though this is a highly specialized and modern language design question I know Charles Babbage would have understood it and I suspect he would have had an opinion.

A while ago I wrote a post about Babbage’s difference engine. The difference engine is basically an adder that calculates successive values of a polynomial by doing simple addition, using a technique called divided differences. This is described in full detail in that post but here’s a summary. Take a sequence of values of values of a polynomial, for instance the first four squares,

You can then calculate the next values using just addition by calculating the *differences* between the initial values. To get the differences for a sequence you subtract each pair of values,

Do it once and you get the *first* differences. Do the same thing to the first differences and you get the *second* differences,

Do the same thing to the second differences and you get the *third* differences,

In this case there’s just one and it’s 0 so we can ignore it. Once you have all the differences you can calculate the next value of the polynomial by adding them together from right to left,

There it is, 16, the next value in the sequence. At this point we can just keep going,

This technique works for any polynomial. The difference engine was given its name because it is a mechanical implementation of it.

Already you may suspect how this can be relevant to ranges in haskell. You can generalize ranges to *n* step values by using divided differences on the step values to generate the rest of the sequence. With two step values the result of this is a linear sequence, as you would expect (`polyEnumFrom`

is my name for the generalized range function),

`ghci> polyEnumFrom [0, 1]`

`[0, 1, 2, 3, 4, 5, 6, ...]`

`ghci> polyEnumFrom [4, 6]`

`[4, 6, 8, 10, 12, 14, 16, ...]`

`ghci> polyEnumFrom [10, 9]`

`[10, 9, 8, 7, 6, 5, 6, ...]`

Give it three elements, for instance the squares from before, and the result is a second-order polynomial,

`ghci> polyEnumFrom [0, 1, 4]`

`[0, 1, 4, 9, 16, 25, 36, ...]`

Similarly with the cubes,

`ghci> polyEnumFrom [0, 1, 8, 27]`

`[0, 1, 8, 27, 64, 125, 216, ...]`

There is nothing magical going on here to recognize the squares or cubes here, it’s a completely mechanical process that uses subtraction to get the differences and addition to then continue the sequence past the step values. It won’t give you the squares though, only polynomials. If you try you get

`ghci> polyEnumFrom [1, 2, 4, 8, 16]`

`[1, 2, 4, 8, 16, 31, 57, ...]`

For any list of 3 step values there is exactly one polynomial of order 2 or less that yields those values; an order-2 polynomial has 3 free variables so there’s no room for more than one solution. The result of a 3-step range is the sequence of values produced by that order-2 polynomial. In general, the result of a multi-step range with *n* steps is the sequence of values generated by the unique polynomial of order at most *n*-1 whose first *n* values are the steps. Conveniently, if the steps are integers the polynomial is guaranteed to only produce integers.

Babbage built the difference engine because it was really important then be able to produce tables of function values by calculating successive values of polynomials. I think he would have appreciated the power of a language where you can condense the work of generating an approximate table of sine values into a single line,

`take 10 (polyEnumFrom [0, 0.0002909, 0.0005818, 0.0008727])`

which yields,

`0.0000000`

`0.0002909`

`0.0005818`

`0.0008727`

`0.0011636`

`0.0014545`

`0.0017454`

`...`

(The four values given to `polyEnumFrom`

are the first four sine values from the table.) Of course, Babbage did lose interest in the difference engine himself and preferred to work on the more general analytical engine. If he did see haskell he would probably quickly lose interest in ranges and move on to more general advanced features. But I digress.

Is a shorthand for sequences of polynomial values actually a useful feature in a language? I doubt it. But I like the idea that a corner of a modern language intersects with Babbage and the difference engine. Maybe it would make sense as a little easter-egg tribute to Babbage, a difference engine hiding behind a familiar language construct. I quite like the idea of that.

If you want to play around with it I’ve put an implementation in a gist. For something that might seem fairly complex the implementation is simple – `polyEnumFrom`

and its helpers is just 13 lines. Haskell is an elegant language.