Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you have to cons with a null to get a proper list in scheme?

Tags:

scheme

racket

I realize this is a total n00b question, but I'm curious and I thought I might get a better explanation here than anywhere else. Here's a list (I'm using Dr. Scheme)

> (list 1 2 3)
(1 2 3)

Which I think is just sugar for this:

> (cons 1 (cons 2 (cons 3 null)))
(1 2 3)

This, on the other hand, does something else:

> (cons 1 (cons 2 3))
(1 2 . 3)

My questions is, why is that different? What's the point of requiring the null at the end of the list?

like image 582
dsimard Avatar asked Nov 06 '09 19:11

dsimard


1 Answers

The definition of a list is recursive.

 1. The null list (empty list) is a list
 2. A list is made up of an item cons a list

So these are lists:

1. null  => ()  --read as empty list
2. cons 3 null  =>  (3)
3. cons2 (cons 3 null)  => (2, 3) 

The last example you gave cons 2 3 does not conform to this list definition so its not a list. That is cons accepts an item and a list. 3 is not a list.

like image 100
Vincent Ramdhanie Avatar answered Oct 20 '22 20:10

Vincent Ramdhanie