cse 341 -- s. tanimoto lisp-2 - 1 lisp s-expressions: atoms every lisp object is either an atom or a...

12
CSE 341 -- S. Tanimoto 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE, A-SYMBOL 1, 5.7, 3/5 Many other Lisp data objects are considered to be atoms (even strings and arrays are atoms!).

Upload: august-hunt

Post on 12-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

1

Lisp S-Expressions: ATOMs

Every Lisp object is either an ATOM or a CONS

Symbols and numbers are kinds of atoms:X, APPLE, A-SYMBOL1, 5.7, 3/5

Many other Lisp data objects are considered to be atoms (even strings and arrays are atoms!).

Page 2: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

2

Lisp S-Expressions: CONSes

Every Lisp object is either an ATOM or a CONS

A CONS represents an association or pairing of two other Lisp objects.

(A . B) (APPLE . RED)(PI . 3.14159)(X . (Y . Z))

Page 3: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

3

Lisp S-Expressions: ListsWe define lists as follows:

The symbol NIL is a list; it’s the empty list.This list is written in list notation as ( )

Any cons having the following structure is a list,(S1 . (S2 . ( ... (Sn . NIL) ... ) ) )where each S1 is either an atom or a cons.This list is written in list notation as(S1 S2 ... Sn)

Page 4: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

4

Examples of Lists

> ’(a b c d e)(A B C D E)> ()NIL> nilNIL> ’()NIL> ’(apple . (banana . (lime . nil)))(APPLE BANANA LIME)

Page 5: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

5

Predicates That Identify Lists

> (atom ’(a b c))NIL> (atom ’x)T > (consp ’(a b c))T> (consp ’x)NIL

Page 6: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

6

List predicates (continued)

> (listp ’(a b c))T> (listp ’x)NIL> (consp ’()) ; NIL is not a cons.NIL> (listp ’()) ; NIL is a list.T> (consp ’(a . b))T> (listp ’(a . b)) ;note listp’s limitation.T

Page 7: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

7

Lisp Tries to Print Conses as Lists

> ’(a . (b . c))(A B . C)> ’(a . nil)(A)> ’((a . b) . (c . d))((A . B)C . D)> ’((nil . nil) . (nil . nil))((NIL)NIL)

Page 8: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

8

Lisp FormsA form is a list whose first element is a symbol that names an operator.

If the first element names a function, then the form is a functional form.

> (+ 1 2 3) ; a functional form6> (functionp #’+)T> (setq x 5) ; a special form5> (functionp #’setq) ;Error!

Page 9: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

9

Evaluation of Functional Forms

A functional form is evaluted as follows:

If there are any arguments in the form, then they are evaluated in left-to-right order.

The number of arguments is compared with the number permitted by the function named by the first element of the form.

If the number is compatible, then the function is applied to the values of the arguments.

Page 10: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

10

QUOTEUnlike functional forms, special forms are not required to have their arguments evaluated.

QUOTE is a special form that returns its argument unevaluated.

> (quote (+ 1 2 3))(+ 1 2 3)> (quote x)X> ’(+ 1 2 3)(+ 1 2 3)> ’xX

Page 11: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

11

SETQSETQ is a special form that evaluates its second argument and assigns that value to the symbol which must be its first argument.

> (setq x (+ 1 2 3))6> (setq x (* x 3))18> (setq y ’(+ 1 2 3))(+ 1 2 3)> (setq y (rest y))(1 2 3)> (setq y (first y))1

Page 12: CSE 341 -- S. Tanimoto Lisp-2 - 1 Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,

CSE 341 -- S. Tanimoto Lisp-2 -

12

IFIF is a special form that evaluates its first argument. If the result is NIL is skips its second argument and evaluates and returns its their element if any. If result of evaluating the first element was not NIL, it evaluates the second argument and returns that.

> (setq x 10)10> (if (> x 2) (- x 1) (+ x 1))9