[Haskell-beginners] Numeric Integer vs Integer

Kim-Ee Yeoh ky3 at atamo.com
Wed Sep 23 08:48:13 UTC 2015


On Wed, Sep 23, 2015 at 2:51 PM, goforgit . <teztingit at gmail.com> wrote:

> What about the following?
>
> data List a = Empty | Add a (List a)
>
> What does the a mean and why is it possible to put it there?


In addition to the good answers already given, it helps to think of it this
way:

Here's a list of Bools:

data ListBool = EmptyListBool | AddListBool Bool ListBool

Here's a list of Chars:

data ListChar = EmptyListChar | AddListChar Char ListChar

Here's a list of Ints:

data ListInt = EmptyListInt | AddListInt Int ListInt

Well just look at all that repetition!

Surely there must be a way to keep DRY and abstract over all that?

Let's see: what's common to all of the above? What stays the same? What
changes?

Here's something that tries to express and separate out what's "fixed" and
what's "insert type here":

data ListX = Empty | Add X ListX

We're close.

That almost but doesn't quite work, because Haskell treats capital X as a
concrete type, like Bool and Char and Int.

What we want is a type _variable_. And Haskell gives us that, if we use
lower-case letters:

data List x = Empty | Add x (List x)

The parens is needed to distinguish against

data List x = Empty | Add x List x

which doesn't work for reasons you can probably guess.

Finally, it's convention to use type variables a b c and not x y z.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150923/fe2f6b0f/attachment.html>


More information about the Beginners mailing list