
Thank you guys, very good descriptions given on my question. Together with
your answers and the analogy of it being somewhat similar to something in
C++ made me understand how it works. Again many thanks!
On Wed, Sep 23, 2015 at 10:48 AM, Kim-Ee Yeoh
On Wed, Sep 23, 2015 at 2:51 PM, goforgit .
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners