Title: LISP Part I
1LISP (Part I)
- Introduction
- LISP An Overview
- Control of LISP Evaluation
- Programming in LISP
- Summary
2Introduction
- LISP has about 40 years of existence
- Originally designed for symbolic computing
- LISP is functional (based on recursive
functions) - Examples of applications
- Search engines
- Pattern matchers
- Theorem provers
- Rule-based expert systems
- Semantic networks
3LISP (Part I)
- Introduction
- LISP An Overview
- Control of LISP Evaluation
- Programming in LISP
- Summary
4Overview
Syntax Basic elements are symbolic expressions
(s-expressions)
Letters Numbers Non-alphanumeric characters
Atoms Lists
s-expressions
Sequence of atoms or lists
5Atoms and Lists
Examples of atoms 3.1416 100 x nil Examples of
lists (1 2 3 4) (tom mary john joyce) (a (b c)
(d (e f))) ()
6Lists
LISP uses lists and atoms to represent both
programs and data.
- ? ( 7 9)
- ? 63
- When given a list
- LISP evaluates the first element as the name of a
function - Evaluates the remaining elements as arguments
- LISP returns the result of applying the function
to the - arguments
7Examples
? ( ( 2 3) 5) ? t ? (a b c) Error invalid
function a
- ? ( 14 5)
- ? 19
- ? ( 1 2 3 4)
- ? 10
- ? (- ( 3 4) 7)
- ? 0
- ? ( ( 2 5) (- 7(/ 21 7)))
- ? 28
8Figure 15.1 Tree diagram of the evaluation of a
simple LISP function.
9Predefined Functions in LISP
? (list 1 2 3 4 5) ? (1 2 3 4 5) ? (nth 0
(a b c d)) a ? (nth 2 (list 1 2 3 4 5)) ?
3 ? (nth 2 ((a 1)(b 2)(c 3)(d 4))) ? (c 3)
? (length (a b c d)) ? 4 ? (member 7 (1 2
3)) ? nil ? (null ()) ? t
10LISP (Part I)
- Introduction
- LISP An Overview
- Control of LISP Evaluation
- Programming in LISP
- Summary
11Quote
To avoid evaluating an argument we use the symbol
or the function quote. ? (quote (a b c))
? (a b c) ? (a b c)
? (a b c) ? (quote ( 1
3)) ? ( 1 3) ? ( 1 3)
? ( 1 3)
12Examples
? (list ( 1 2) ( 3 4)) ? (3 7) ? (list
( 1 2) ( 3 4)) ? (( 1 2) ( 3 4))
13Eval
The opposite of quote is eval. It evaluates the
argument twice. ? (eval (quote ( 2 3))) ?
5 ? (eval (list 2 5 )) ? 10
14LISP (Part I)
- Introduction
- LISP An Overview
- Control of LISP Evaluation
- Programming in LISP
- Summary
15Creating New Functions
New functions are built using the word
defun. (defun square (x) ( x x)) 1st
argument name of the function 2nd argument
list of arguments The rest constitute the body of
the new function ? (square 5) ? 25
16Another Example
(defun hypotenuse (x y) (sqrt ( (square
x) (square y))))
? (hypotenuse 4 3) ? 25
17Conditionals
Implementing conditional branches (cond
(ltcondition 1gt ltaction 1gt) (ltcondition
2gt ltaction 2gt)
(ltcondition ngt ltaction ngt)) (defun
absolute-value (x) (cond ((lt x 0) (-
x)) ((gt x 0) x )))
18Arithmetics
? (gt 17 4) ? t ? (lt 8 ( 4 2)) ? nil ?
(oddp 3) checks whether argument is odd or
not ? t ? (minusp 6) checks if argument
is less than zero ? nil
19Arithmetics
? (numberp 17) checks whether argument is
numeric ? t ? (zerop 6) true
if argument is zero ? nil ? (plusp 10)
true if argument is strictly greater than
zero ? t
20Logical Connectives
Logical connectives and, or, not. Evaluation is
left to right. ? (and (oddp 2) (print
hello)) ? nil ? (and (oddp 3) (print
hello)) ? hello ? (or (oddp 2) (print
hello)) ? hello
21LISP (Part I)
- Introduction
- LISP An Overview
- Control of LISP Evaluation
- Programming in LISP
- Summary
22Summary
- LISP works on symbolic expressions (atoms and
lists). - Lists can be used to represent programs and data.
- Function quote avoids evaluating the arguments
of - the function.
- Function eval works contrary to quote it
evaluates - the argument twice.
- New functions are built using the word defun.