[Haskell-beginners] Empty list and null

Francesco Ariis fa-ml at ariis.it
Sat Aug 18 09:36:56 UTC 2018


Hello Trent,

On Sat, Aug 18, 2018 at 02:13:25AM -0700, trent shipley wrote:
> Why does Haskell so often seem to treat [] as a general null.
> 
> For example I know 0 : 1 : [] gives [0, 1].
> 
> But shouldn't it produce a type fault in a consistent world?
> 
> Int:Int:List isn't properly a list.  It mixes types.

`:` is not syntactic sugar, but a data constructor and behaves like one!

    λ> :type (:)
    (:) :: a -> [a] -> [a]

"Give me an `a` and a list of `a`, I will return a list."

The `empty list` ([]) is polymorphic:

    λ> :t []
    [] :: [a]

(it could be an empty list of strings, of ints, of dromedaries),
so `3:[]` is well typed.

Note that `3:[]:4` will not type-check and that to build a list, you
*have* to start with a `[]`.



More information about the Beginners mailing list