Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lisp: Representation of Data

Similar presentations


Presentation on theme: "Lisp: Representation of Data"— Presentation transcript:

1 Lisp: Representation of Data
Atoms: Symbols, numbers, functions, strings, arrays, structures. Conses: Lists, dotted pairs. Forms: functional forms, special forms, macro forms. Evaluation of functional forms. Special forms: QUOTE, SETQ, IF CSE (c) S. Tanimoto, Lisp Representation

2 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!). CSE (c) S. Tanimoto, Lisp Representation

3 Lisp S-Expressions: CONSes
(Again, 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 ) (X . (Y . Z)) CSE (c) S. Tanimoto, Lisp Representation

4 Lisp S-Expressions: Lists
We 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 Si is either an atom or a cons. This list is written in list notation as (S1 S Sn) CSE (c) S. Tanimoto, Lisp Representation

5 CSE 415 -- (c) S. Tanimoto, 2002 Lisp Representation
Examples of Lists > ’(a b c d e) (A B C D E) > () NIL > nil > ’() > ’(apple . (banana . (lime . nil))) (APPLE BANANA LIME) CSE (c) S. Tanimoto, Lisp Representation

6 Predicates That Identify Lists
> (atom ’(a b c)) NIL > (atom ’x) T > (consp ’(a b c)) > (consp ’x) CSE (c) S. Tanimoto, Lisp Representation

7 List predicates (continued)
> (listp ’(a b c)) T > (listp ’x) NIL > (consp ’()) ; NIL is not a cons. > (listp ’()) ; NIL is a list. > (consp ’(a . b)) > (listp ’(a . b)) ;note listp’s limitation. CSE (c) S. Tanimoto, Lisp Representation

8 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) CSE (c) S. Tanimoto, Lisp Representation

9 CSE 415 -- (c) S. Tanimoto, 2002 Lisp Representation
Lisp Forms A 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. > ( ) ; a functional form 6 > (functionp #’+) T > (setq x 5) ; a special form 5 > (functionp #’setq) ; Error! CSE (c) S. Tanimoto, Lisp Representation

10 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. CSE (c) S. Tanimoto, Lisp Representation

11 CSE 415 -- (c) S. Tanimoto, 2002 Lisp Representation
QUOTE Unlike functional forms, special forms are not required to have their arguments evaluated. QUOTE is a special form that returns its argument unevaluated. > (quote ( )) ( ) > (quote x) X > ’( ) > ’x CSE (c) S. Tanimoto, Lisp Representation

12 CSE 415 -- (c) S. Tanimoto, 2002 Lisp Representation
SETQ SETQ is a special form that evaluates its second argument and assigns that value to the symbol which must be its first argument. > (setq x ( )) 6 > (setq x (* x 3)) 18 > (setq y ’( )) ( ) > (setq y (rest y)) (1 2 3) > (setq y (first y)) 1 CSE (c) S. Tanimoto, Lisp Representation

13 CSE 415 -- (c) S. Tanimoto, 2002 Lisp Representation
IF IF is a special form that evaluates its first argument. If the result is NIL it skips its second argument and evaluates and returns its third 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 CSE (c) S. Tanimoto, Lisp Representation


Download ppt "Lisp: Representation of Data"

Similar presentations


Ads by Google