0% found this document useful (0 votes)
135 views4 pages

Checking Makes Recursion More Useful.: Advantages of Lisp

Lisp has several advantages including recursion, garbage collection, uniform representation where programs and data are the same, and interactive development. Lisp code uses parentheses to enclose functions and arguments, all operations are function calls, assignment uses setq, and functions always return values. Lisp data includes symbols, numbers, atoms like symbols and numbers, and s-expressions which are recursively defined as atoms or lists of s-expressions. Variable values in Lisp are stored in symbol value cells and can be set using set and setq. Common list functions include car, cdr, append, reverse, length, and cons for constructing new list structure.

Uploaded by

lalitha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views4 pages

Checking Makes Recursion More Useful.: Advantages of Lisp

Lisp has several advantages including recursion, garbage collection, uniform representation where programs and data are the same, and interactive development. Lisp code uses parentheses to enclose functions and arguments, all operations are function calls, assignment uses setq, and functions always return values. Lisp data includes symbols, numbers, atoms like symbols and numbers, and s-expressions which are recursively defined as atoms or lists of s-expressions. Variable values in Lisp are stored in symbol value cells and can be set using set and setq. Common list functions include car, cdr, append, reverse, length, and cons for constructing new list structure.

Uploaded by

lalitha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Advantages of Lisp 

 Recursion: A program can call itself as a subroutine. Dynamic type


checking makes recursion more useful.
 Garbage Collection: automatically recycles memory.
 Uniform Representation: Programs and data are the same.
o Programs can examine other programs.
o Programs can write programs.
o Programs can modify themselves (learn).
o Data structures can contain programs; programs can contain data
structures.
 Interaction: User can combine program writing, compilation, testing,
debugging, running in a single interactive session.

Lisp Code 

Lisp code is based on a few simple rules:

 Parentheses enclose a function name and its arguments: (sqrt x)


 All operations are done by function calls: (+ x y)
 The assignment operator is called setq: 
(setq area (* pi (expt radius 2)))
 Every function call returns a value.

 (defun abs (x)
 (if (> = x 0)
 x
 (- x)))
 Local variables are declared by a let: 
(let (x y) (setq x 3) ... )
 Quick Lisp 

 int myfn (a, b) (defun myfn (a b)
 { int i = 3; float x; (let ((i 3) x)
 ... } ... ))

 { statement; ... } (progn statement ... )

 i = j + 2; (setq i (+ j 2))

 sqrt(x) (sqrt x)

 if ( i > j j > k ) (if (and (> i j)
 (> j k))
 statement1 statement1
 else statement2; statement2)

 for (i=0; i< n; i++) ... (dotimes (i n) ...)

 while ( i < n ) statement; (while (< i n)
 statements)

 printf("%d\n", i); (print i)
Lisp Data 

 Symbols may contain letters, numerals, and some special characters + - * / @


$ % ^ & _ < > ~ . ?

 MASS CS343 WIDGET-ALIGNMENT-SCREW ?X
 Numbers: floating-point, complex, integer (including bignums or big numbers),
rational: 1/3.
 Atoms include symbols and numbers.
 S-expressions (symbolic expressions) are defined recursively as follows:
o An atom is an S-expression.
o If x1 ... xn are S-expressions, then (x1 ... xn ), called a list of x1 ... xn, is an
S-expression.

 ONTOGENY
 (THIS IS A LIST)
 (* PI (EXPT R 2))
 (ALL X (IF (HUMAN X) (MORTAL X)))
 () ((())) ((()())())
The empty list () is equivalent to the symbol NIL.

Variable Values in Lisp 

We can think of a symbol as a data structure that includes a value cell containing


a pointer to the value of the atom. The value of the symbol can be set using the
function SET:

(SET 'PRESIDENT 'JEFFERSON)


If we now evaluate PRESIDENT, we get the value JEFFERSON.

Since the first argument of SET is usually quoted, there is a special function SETQ that
does this automatically.

(SETQ PRESIDENT 'JEFFERSON)

(SETQ RADIUS 5.0)


(* PI (EXPT RADIUS 2))
Combinations of CAR and CDR 

Since combinations of CAR and CDR are frequently used, all combinations up to four


uses of CAR and CDR are defined as functions of the form CxxxR:

(CAAR X) = (CAR (CAR X))

(CADR X) = (CAR (CDR X))

(CADDR X) = (CAR (CDR (CDR X)))

It's worth memorizing common combinations:

CAR or FIRST = First element of a list


CADR or SECOND = Second element
CADDR or THIRD = Third element
CADDDR or FOURTH = Fourth element

Functions FIRST through TENTH are defined in Common Lisp.

List Manipulation Functions 

APPEND makes a new list consisting of the members of its argument lists. APPEND takes


any number of arguments.

(APPEND '(A) '(B)) = (A B)

(APPEND '(A B) '(C D)) = (A B C D)

(APPEND '(A) '(B) '(C)) = (A B C)

REVERSE makes a new list that is the reverse of the top level of the list given as its
argument.

(REVERSE '(A B)) = (B A)

(REVERSE '((A B)(C D))) = ((C D)(A B))

LENGTH returns the length of the top level of the list given as its argument.
(LENGTH '(A)) = 1
(LENGTH '(A B)) = 2
(LENGTH '((A B))) = 1
Constructing List Structure 

The basic function that constructs new list structure is the function CONS.

If Y is a list, then we can think of (CONS X Y) as adding the new element X to the front
of the list Y.

(CONS 'A '(B)) = (A B)

(CONS 'A NIL) = (A)

(CONS 'A '()) = (A)

(CONS '(A) '(B)) = ((A) B)

(CONS 'A 'B) = (A . B)

The following axioms always hold:

1. (CAR (CONS x y)) = x


2. (CDR (CONS x y)) = y

You might also like