Why do we need "zero"?

I do not understand why we need nil[1] when the conssequence (the so-called correct list) of elements. It seems to me that we can achieve the same goal using only the so-called incorrect list ( cons-ed pairs without ending nil). Since Lisps [2] already provided a primitive procedure for distinguishing between a pair?and atom (some implementations even provide atom?), when defining a procedure in a list, for example length, I can do the same with only dashed pairs, as shown below:

(define len
  (lambda (l)
    (cond ((pair? l) (+ 1 (len (cdr l))))
          (else 1) ) ) )

Obviously, we can apply this procedure to the wrong list, for example '(1 . (2 . 3)), to get the expected answer 3, unlike the traditional one (length '(1 2 3)).

I would like to hear any opinions in defense of necessity nil. Thanks in advance.

[1] Let the debate between nil/ nil, '()and be neglected ().

[2] Here, this means the Lisp family of languages.

+4
source share
2 answers

Working with lists without nil(or '()) will be similar to performing arithmetic without zero. Using only pairs without nil, how would we represent an empty list, or a singleton list '(1)?

Deteriorating: since lists should not be lists of atoms, but may contain other lists, how do we present a nested list '(1 2 (3 4))? If we perform the following transformations:

'(3 4) => '(3 . 4)
'(1 2 x) => '(1 . (2 . x)) == '(1 2 . x)

we get:

'(1 2 (3 4)) => '(1 . (2 . (3 . 4))) == '(1 2 3 . 4)

But also:

'(1 2 3 4) => '(1 . (2 . (3 . 4))) == '(1 2 3 . 4)

, nil , , . , , , .

: , first, , rest, , . , , rest , . , , pair?, . nil , , , 1, '(1), '((1)) ..

+20

, "Nothing".

+1

All Articles