Download Dot Notation in Scheme

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

B-tree wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Dot Notation in Scheme
Scheme uses a dot notation as a way to express lists and certain data structures.
Scheme lists are closely related to linked data structures. This paper will explore the
relationships among dot notation, Scheme lists, and linked lists.
Consider the following dot notation, (1 . 2) . Each dotted pair is represented by a node
in a data structure. The node consists of two components. The first element of the dotted
pair is stored in the first component of the node, and the second element of the pair is
stored in the second component of the node. The pair above can be represented as a
single node with the structure below.
1
2
Sometimes a dotted pair has a component that is another dotted pair, or the null list. For
example, the dot notation ( 1 . ( ) ) can be represented as a single node with the structure
below.
1
()
Think of the ( ) inside the box as a null pointer. This is also represented as the list ( 1 ).
Proper lists always end with a null pointer in Scheme. When a component of a dotted
pair is another dotted pair, we represent the component as a pointer to a structure.
Consider the following dot notation, ( 1 . ( 2 . ( 3 . ( ) ) ) ) . This list is represented by the
following data structure.
1
2
3
()
This is also represented as the proper list (1 2 3).
Not every structure specified by dot notation is a proper list. For example ( 1 . ( (2 . 3)
. (4 . 5) ) ) has the following data structure.
1
4
2
5
3
This structure cannot be represented as a proper list since the pointer in the last node is
not null. The structure above is a tree, and as a result cannot be represented as a proper
list. A proper list is represented as a simple linked list where the last pointer is null.
Scheme provides two operations for accessing the data in a node or dotted pair These
operations are car and cdr. The “car” operation returns the first component of the pair,
and “cdr” returns the second component. In the example above (car ‘(1 . ( ( 2 . 3 ) . ( 4 .
5) ) ) ) is 1, and (cdr ‘(1 . ( ( 2 . 3 ) . ( 4 . 5) ) ) ) is ((2 . 3).(4 . 5)) . Scheme also provides
the “cons” operation for creating a dotted pair. For example, consider (cons 3 ‘(1 2 )).
The list (1 2) has the following structure.
1
2
()
After the cons operation, the structure looks like this.
3
1
2
()
It is represented as the proper list (3 1 2). We could build the list by coding (cons 3 (cons
1 ( cons 2 ( )))).