LISP is a programming language invented in 1958 that uses two simple data structures - atoms and lists. It heavily relies on recursion and functional programming. LISP defines all data as lists and represents programs as nested function calls, allowing for dynamic typing and easy abstraction. It introduced many concepts still used in modern languages, including conditionals, recursion, dynamic typing, garbage collection, and representing programs as mathematical expressions.
2. LISP
• Invented by John McCarthy (1958)
• Two simple data structure (atoms and lists)
• Heavy use of recursion
• Interpretive language
• Variations
– Scheme
– Common Lisp (de facto industrial standard)
• Most widely used AI programming language
• Functional Programming Paradigm
• Low maintenance overhead
3. • Atoms (Can’t be sub-divided) may be;
• Numbers: (real 1.0, integer 1)
• Symbols: a consecutive sequence of characters (no space)
e.g. a, x, price-of-beef
• Two special symbols: T and NIL for logical true and false
• Strings: a sequence of characters bounded by double
quotes e.g. "this is red"
Valid Objects
4. • Lists: A list of atoms and/or lists, bounded by ( and ). For
example
(a b c)
(a (b c))
• Top elements of a list – first level
Example:
Top elements of list (a b c) are a, b, and c
Top elements of list (a (b c)) are a and (b c)
‘nil’ stands for empty list, same as ()
Valid Objects
5. • Its also a list
• Uses prefix notation:
(function-name arg1,arg2 ... argN)
• It returns function value for the given list of arguments
• Functions are either provided by LISP function library or
defined by the user
Function Calls
7. 1) Evaluating an atom
• Numerical and string atoms evaluate to themselves
• Symbols evaluate to their values if they are assigned
values, otherwise return Error
• The values of T and NIL are themselves
Evaluation of S-expression
8. • Examples :
> (sqrt x)
Error: The variable
X is unbound
> (+ (sqrt 4) 4.0)
6.0
> (+ (/ 3 5) 4)
23/5
Evaluation of S-expression
3/5+4
9. 3) To assign a value to a symbol use: setq, set, setf
•‘setq’ is a special form of function (with two arguments)
The first argument is a symbol which will not be evaluated
The second argument is a S-expression, which will be
evaluated
The value of the second argument is assigned to be the
value of the first argument
> (setq x 3.0)
3.0
> x
3.0
Evaluation of S-expression
11. • To forbid evaluation of a symbol use: quote or ’
> (quote x)
x
> 'x
x
> (setq z 'x)
x
12. Functions
• Math functions
– +, -, *, /, exp, expt, log, sqrt, sin, cos, tan, max, min
• Functions for creating lists or ‘List Constructors’
– Cons, list and append
> (cons 'x L) ; insert symbol x at the front of list L
(X A B C) > (setq L '(A B C))
> (list 'a 'b 'c) ; making a list with the arguments as its elements
(A B C) ; if a, b, c have values A, B, C, then (list a b c)
; returns list (A B C)
> (append '(a b) '(c d)) ; appends one list in front of another
(A B C D)
L is a list containing elements
(A,B,C)X is inserted at the front/top of the list
13. Functions (cont’)
• Selectors
– first, rest, nth, car, cdr…
> (first '(a s d f)) ;returns the first element of a list
a
> (rest '((a s) d f)) ;deletes the first element from the
;list and returns the rest as a list
(d f)
> (first '((a s) d f))
(a s)
> (rest '((a s) (d f))
((d f))
14. > (setq L '(A B C))
(A B C)
> (car L)
; returns the first top level element of list L
A
> (cdr L)
; returns the rest of list L
(B C)
15. Functions
> (reverse L) ; reverses a list
(C B A)
> (length L) ; returns the length of list L
3
List has elements
(A B C)
16. • Conditional control:
– if, when and cond
(if <test> <then> <else>)
> (setq SCORE 78)
> 78
> (if (> score 85) ‘HIGH
(if (and (< score 84) (> score 65)) ‘MEDIUM ‘LOW))
> MEDIUM
Control Flow
score<84 and score>65
17. Predicates
• A special function which returns NIL if
the predicate is false, T or anything
other than NIL, otherwise
• We can use the following operators to
build the predicates
– =, >, <, >=, <= for numeric values
– eq or equal for others
– atom: test if x is an atom
– listp: test if x is a list
18. • A list can be viewed as a set whose members are the top
elements of the list
> (member 'b L)
(B C)
> (member ‘b (cons 'b L))
(B A B C)
> (member x L)
NIL ; if no, returns NIL
> (union L1 L2) ; returns the union of the two lists
> (intersection L1 L2) ; returns the intersection of the two lists
> (set-difference L1 L2) ; returns the difference of the two lists
Set Operations
Test if symbol b is a member (a top
element) of L; if yes, returns the sublist of L
starting at the first occurrence of symbol b
List L has elements (A B C)
19. Defining LISP functions
(defun func-name (arg-1 ... Arg-n) func-body)
Example:
> (defun y-plus (x) (+ x y)) ;definition of y-plus
> (setq y 2)
> (y-plus 23)
25
20. • Recursion
– Main tool used for iteration
– There is a limit to the depth of the recursion,
and it depends on the version of LISP
– Use trace to watch recursion
– Example:
(defun power (x y)
(if (= y 0) 1 (* x (power x (1- y)))))
21. examples:
(defun member (x L)
(cond ((null L) nil) ; base case 1: L is empty
((equal x (car L)) L) ; base case 2: x=first(L)
(t (member x (cdr L))) ; recursion: test if x is in rest(L)
))
(defun intersection (L1 L2)
(cond ((null L1) nil)
((null L2) nil)
((member (car L1) L2)
(cons (car L1) (intersection (cdr L1) L2)))
(t (intersection (cdr L1) L2))
))
> (intersection '(a b c) '(b a b c))
> (a b c)
> (intersection '(b a b c) '(a b c))
> (b a b c)
22. • Iteration: dotimes and dolist
– (dolist (x L result) body)
• for each top level element x in L, do body(x);
• x is not equal to an element of L in each iteration, but rather x
takes an element of L as its value;
– (dotimes (count n result) body)
• do body n times. count starts with 0, ends with n-1
– Note: result is optional, to be used to hold the computing
result. If result is given, the function will return the value of
result, returns NIL, otherwise. (may change global
variables as side effects.)
23. Arrays
• Data type to store expressions in places
identified by integer indexes.
• (setf linear-array (make-array ‘(4)))
• A single dimension array by the name of
linear-array with indices from 0 to 3.
• (setf linear-array (make-array 4))
• (setf chess-board (make-array ‘(8 8)))
24. • Other functions in LISP library
1) Predicates:zerop, plusp, evenp, oddp, integerp, floatp
2) Logical connector: and, or, not
3) Rounding: floor, ceiling, truncate, round
4) Others:
max, min, abs, sqrt, 1+ (add 1), 1- (minus 1)
(exp number) (base-e exponential)
(expt Base-number Power-Number)
(log number & Optional base-number)
(isqrt number) Returns the greater integer less than or equal to
the exact positive square-root of the number.
(signum number) Returns -1, zero, or 1 according if the
number is negative, zero, or positive.
26. What made Lisp Different
• Conditionals: such as if-then-else construct.
• Function types: where functions are just like integers
and strings
• Recursion: first language to support it.
• Dynamic typing: all variable are pointers.
• Garbage-Collection.
• Programs composed of expressions.
• A symbol type.
• The whole program is a mathematical function
Editor's Notes
S-expression
From Wikipedia, the free encyclopedia.
An S-expression (S stands for symbolic) is a convention for representing data or an expression in a computer program in a text form. S-expressions are used in the Lisp programming language and Lisp-derived languages such as Scheme and DSSSL, and as mark-up in communications protocols like IMAP and John McCarthy&apos;s CBCL. The details of the syntax and supported data types vary in the different languages, but the most common feature is the extensive use of prefix notation with explicit use of brackets (affectionately known as Cambridge Polish notation).
S-expressions are used for both code and data in Lisp (see McCarthy Recursive Functions of Symbolic Expressions at http://www-formal.stanford.edu/jmc/recursive/recursive.html ). S-expressions were originally intended only as machine representations of human-readable M-expressions, but Lisp programmers soon started using S-expressions as the default notation.
S-expressions can either be single objects such as numbers, LISP atoms including the special atoms nil and t, or cons pairs, written as (x . y). Longer lists are made up of nested cons pairs, for example (1 . (2 . (3 . nil))) which can also be written more intelligibly as (1 2 3).
Program code can be written in S-expressions, using prefix notation. An extra piece of syntactic sugar for writing Lisp programs is that the common expression (quote x) can be written with the abbreviation &apos;x.
Example in Lisp:
(defun factorial (x) (cond ((eq x 1) 1) (t (* x (factorial (- x 1))))))
Example in Scheme:
(define (factorial x) (if (= x 1) 1 (* x (factorial (- x 1)))))
Member (x) we check that x is member of L (list)..
Cons is adding element in new list.
Program composed of expression: Lisp programs are trees of expression, each of which returns a value.
Symbol types: symbols are effectively pointer to strings stored in a hash table
The whole langauge is there: there is no real distciton between read-time, compile-time and runtime. You can compile or run code while reading, read or run code while compiling , and read or compile code at runtime. That is, programs are expressed directly in the parse trees that get build behind the scenes when other languages are parsed, and these trees are make of lists, which are Lisp date structures. This provides a very powerful feature allowing the programmer to write programs that write programs. The most common way this is done in lisp is through Macros, which I will discuss later.
Comments
# re: The XML Conspiracy
Rick Schaut
I remember sitting around with a bunch of grad students talking about LISP, when one asked me, &quot;You know what &apos;LISP&apos; stands for, don&apos;t you?&quot; &quot;No,&quot; I said. &quot;Lots of Insipid, Stupid Parentheses!&quot; he exclaimed. To which, another grad student replied, &quot;Silly! The &apos;S&apos; stands for Silly!“
Lost In a Sea of Parentheses