I\'m reading A Gentle Introduction to Haskell (which is not so gentle) and it repeatedly uses the :
operator without directly explaining what it does.
S
It is the type constructor for lists. It is no different from any other type constructor like Just
or Left
, except that it is infix. Valid type constructors can either be words starting with a capital letter, or symbols starting with a colon.
So you can define infix constructors for your own data types. For example:
data MyList a = a :> MyList a
| Empty
in the above code we define a type called MyList
with two constructors: the first is a weird-looking constructor :>
which takes an element and another MyList a
; the second is an empty constructor Empty
which is equivalent to []
in Haskell's native lists.
The above is equivalent to :
data MyList a = Cons a (MyList a)
| Empty