[Haskell-beginners] Parametrizing [] as an instance of the Functor type class

bergey at alum.mit.edu bergey at alum.mit.edu
Wed Jan 6 14:50:42 UTC 2016


On 2016-01-05 at 08:59, Olumide <50295 at web.de> wrote:
> On 01/01/2016 19:41, Alexander Berntsen wrote:
>> Here we use [] both on type and term level. On type level we use it to
>> mean a list of 'a's, and on term level we use it to mean the empty list.
>
> Out of curiosity, is [] defined as type constructor _and_ term level at 
> the library level or in the language/compiler? (BTW, google tells me 
> "term-level" has a special meaning that I do not yet know.)

The special syntax of [] requires that the compiler (specifically, the
parser) treat lists specially.

We could define our own data type that behaves like lists,

List a = Nil | Cons a (List a)

but writing out literal lists would be a little clunky.  It's nice that
we can write:

[]
[1]
[1,2,3]

instead of:

Nil
Cons 1 Nil
Cons 1 (Cons 2 (Cons 3 Nil))

The [1,2,3] syntax requres that the Haskell parser be aware of this
type.

At the type level, we can write [Int], [Char], and so forth, instead of
List Int, List Char.  This also requires support in the parser.

bergey


More information about the Beginners mailing list