<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 23, 2015 at 2:51 PM, goforgit . <span dir="ltr"><<a href="mailto:teztingit@gmail.com" target="_blank">teztingit@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>What about the following?<br><span style="font-family:monospace,monospace"><br>data List a = Empty | Add a (List a)</span><br><br></div>What does the a mean and why is it possible to put it there?</blockquote></div><br></div><div class="gmail_extra">In addition to the good answers already given, it helps to think of it this way:<br><br></div><div class="gmail_extra">Here's a list of Bools:<br><br></div><div class="gmail_extra">data ListBool = EmptyListBool | AddListBool Bool ListBool<br><br><div class="gmail_extra">Here's a list of Chars:<br><br></div>data ListChar = EmptyListChar | AddListChar Char ListChar<br><br><div class="gmail_extra">Here's a list of Ints:<br><br></div>data ListInt = EmptyListInt | AddListInt Int ListInt<br><br></div><div class="gmail_extra">Well just look at all that repetition!<br><br></div><div class="gmail_extra">Surely there must be a way to keep DRY and abstract over all that?<br><br></div><div class="gmail_extra">Let's see: what's common to all of the above? What stays the same? What changes? <br><br>Here's something that tries to express and separate out what's "fixed" and what's "insert type here":<br><br></div><div class="gmail_extra">data ListX = Empty | Add X ListX<br><br></div><div class="gmail_extra">We're close.<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">That almost but doesn't quite work, because Haskell treats capital X as a concrete type, like Bool and Char and Int. <br><br>What we want is a type _variable_. And Haskell gives us that, if we use lower-case letters:<br><br></div><div class="gmail_extra">data List x = Empty | Add x (List x)<br><br></div><div class="gmail_extra">The parens is needed to distinguish against<br><br>data List x = Empty | Add x List x<br><br></div><div class="gmail_extra">which doesn't work for reasons you can probably guess.<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Finally, it's convention to use type variables a b c and not x y z.<br></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">-- Kim-Ee</div></div>
</div></div>