Developing Natural Numbers

Fritz Ruehr • Willamette University CS 451 • Spring 2006

This lecture shows how a data type for natural numbers and some useful functions on them (comparison, conversion, arithmetic and iteration) might be developed in Haskell. It is structured to emphasize the kinds of incremental changes one makes when developing code in Haskell or any other language which promotes exploratory programming. Normally there are two kinds of steps here, one in which we extend the code with some new feature or capability and one in which we modify an existing implementation to utilize some alternative approach or style. For the extension steps, we show the old code with new additions highlighted (old in grey, new in blue). For the modification steps, we place the old and new code side-by-side, with the changed parts highlighted in black on the left and blue on the right. This approach should help you develop a feel both for the overall development process and for specific language features and techniques that are used.

In addition to the code extension and comparison features, we also include sample inputs and responses in green: you should be able to load the code and try out these examples (and others) in a Hugs window. The final code is gathered and consolidated at the end of the lecture into two files which represent a long-winded but simpler approach and a concise but subtle one.

Contents