Universitetet i Linköping Institutionen för datavetenskap

advertisement
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
TDDC65 Artificial intelligence and Lisp
Lecture 1 in Lisp
Allegro Common Lisp
LISP-constructions
(chapter 2 Haraldssons book)
One window for editing in emacs i a lisp mode
One window for evaluating Lisp expressions
Embedded in emacs
numbers
integers
floating point numbers
ratio
operations on numbers
expressions
truth values
relational expressions
logical expressions
conditional expressions
function definitions
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
A Lisp program is a set of functions.
What is a Lisp program
Every expression can be evaluated
There is a top loop: read-eval-print
CL-USER(3): (+ 3 4)
7
There is no main-function. A function is an
entity of its own and can be tested and called
in isolation.
CL-USER(4): (+ (* 3 4) 5)
17
A problem is solved by a number of functions, which normally are saved on one or
several files to constitute a “full” program.
CL-USER(5): (setq my-pi 3.14)
3.14
Functions are loaded into the evaluator,
where they are used.
CL-USER(6): (+ 2 my-pi)
5.14
Next slide illustrate how a problem is solved
by a number of functions in a lisp style.
CL-USER(7): (defun n-pi (n) (* n my-pi))
n-pi
CL-USER(8): (n-pi 3)
9.42
The problem is to calculate the square root of
a number by Newtons method.
We show a recursive solution, which gives an
approximate answer.
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Newtons method (sec 3.4)
guess
n/guess
average
of these two numbers
2/1=2
(2+1)/2=1.5
2/1.5=1.333.. (1.5+1.33..)/2=1.4167
1.4118..
1.4142...
1
1.5
1.4167..
1.4142..
We terminate when |guess2-n| < 0.0001
Numbers
integers 15, -23, 123456789123456 (very
large numbers)
floating point numbers 3.45, - 15.34, 4.5E+4
----------------------------------------------------------------------------------
(defun my-sqrt (n)
(sqrt-newton n 1))
ratio 5/3 -13/99
(defun sqrt-newton (n guess)
(if (good-enough? n guess)
guess
(sqrt-newton n (improve-guess n guess))))
Numbers can be mixed in expressions.
No declarations are necessary.
(defun good-enough? (n guess)
(< (abs (- (square guess) n)) 0.0001))
(defun improve-guess (n guess)
(average (/ n guess) guess))
(defun average (x y) (/ (+ x y) 2))
(defun square (x) (* x x))
---------------------------(my-sqrt 2) => 1.4142..
(my-sqrt 9) => 3.000..
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Expressions
Definition functions
In Lisp an expressions (form) is:
data object or a name/variable
(function expr expr ... expr)
Other languages
(defun square (x) (* x x))
parameter list
formal parameters
Lisp
function body
infix notation
3+4
x>y
priority
(3+4)*5
3 + 4*5
prefix notation
sin(x)
print(x)
read
key words
go 10
if x<y then 1 else 2
do ....
for ...
(+ 3 4)
(> x y)
(defun sum-of-squares (x y)
(+ (square x) (square y)))
(* (+ 3 4) 5)
(+ 3 (* 4 5))
(square 5)
=> 25
(sin x)
(print x)
(read)
(go 10)
(if (< x y) 1 2)
(do ....
(for ...
actual parameters or argument
(sum-of-squares (+ 3 1) 6)
=> 42 + 62 = 52
actual parameter evaluates and binds to
corresponding actual parameter
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Operations on numbers
(+ 3 4)
(+ 3 4 5 6 7)
(- 10 5)
(- my-pi)
(* 2 3 4)
(/ 10 2)
(/ 10 7)
(+ 3 4.5 5)
(+ 2.5 4.5)
=> 7
=> 25
=> 5
=> -3.15
=> 24
=> 5
=> 10/7
=> 12.5
=> 7.0
(/ 10.0 7)
(float (/ 10 7))
=> 1.42857...
=> 1.42857...
(1+ 5)
(1- 5)
=> 6
=> 4
(max 2 5 3)
=> 5
(truncate 10.7)
(round 10.7)
=> 10
=> 11
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Truth values
- nil
- all other values
usually t
false value
true value
Relational functions
(< 5 10)
(> 5 10)
(=10 (+ 5 5))
(>= 5 5)
(/= 5 10)
=> t
=> nil
=> t
=> t
=> t
Logical functions
(and (> 10 5) (= 2 (- 4 2)))
(or (> 10 5) (< 10 5))
(not nil)
=> t
=> t
=> t
Universitetet i Linköping
Institutionen för datavetenskap
Anders Haraldsson
Conditional expression
x
if x<=0
f(x,y) = x2+y if 0<x<10
x+y2 if x>=10
all other cases
(defun f (x y)
(cond ((<= x 0) x)
((and (> x 0) (< x 10))
(+ (* x x) y))
((>= x 10) (+ x (* y y)))))
t
{
Observe that cond is an expression and
returns a value.
(+ 10 (cond ((= x 0) 20) (t 30)))
Two-ways conditional expressions
(cond
((< x 0) (- x))
(t x))
can be transformed to
(if (< x 0) (- x) x)
(if predicate-expr expr expr)
An expression giving a truth-value is
called a predicate.
Download