On Wed, Sep 23, 2015 at 2:51 PM, goforgit . <teztingit@gmail.com> wrote:What about the following?What does the a mean and why is it possible to put it there?
data List a = Empty | Add a (List a)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 ListBoolHere's a list of Chars:data ListChar = EmptyListChar | AddListChar Char ListCharHere's a list of Ints:data ListInt = EmptyListInt | AddListInt Int ListIntWell 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 ListXWe'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 xwhich 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