[Haskell-beginners] Empty list and null

Ut Primum utprimum at gmail.com
Sat Aug 18 09:34:23 UTC 2018


If you consider the type of the operator *(:)* you have:

Prelude> :t (:)
(:) :: a -> [a] -> [a]

So it takes an element of type a and a list. So if you write 1:[2,3,4] the
type is correct, because you give an integer 1 and a list of integers
[2,3,4]. You will obtain the list of integers [1,2,3,4]. Similarly, writing
1:[] is correct and gives you [1] as result.

Then, if you write
0 : 1 : []
(as in your example), is the same as
0 : (1 : [])
so it means 0 : [1], which is [0,1]. So, the operator (:) is right
associative.

If it was left associative, your example would give an error. Indeed
(0 : 1) : []
is not correct in Haskell.

Furthermore, your final examples are both false:

Prelude> [] == [] : []
False

[[], []] == [] : []
False

The following is True:
Prelude> [[]] == [] : []
True

Indeed if you write [] : [] youy mean you want to build a list whose first
element (head) is [] and whose "tail" (i.e. the rest of the list) is the
empty list.
So, if 1:[] is [1], then []:[] is [[]].

Ut

2018-08-18 11:13 GMT+02:00 trent shipley <trent.shipley at gmail.com>:

> 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.
>
> I expect something like:
>
> Let GSN mean general_scalar_null.
>
> 1 : 2 : GSN  -- works
>
> 1 : 2 : []  -- type fault, cannot mix int and empty list in the same list.
>
> And why does [] == [] : []
> instead of [[], []] == [] : []
>
> What sorts of nullity are there in core Haskell?
>
> Trent.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180818/0eebd5a6/attachment-0001.html>


More information about the Beginners mailing list