# Universitetet i Linköping Institutionen för datavetenskap

```Universitetet i Link&ouml;ping
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Institutionen f&ouml;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
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Institutionen f&ouml;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
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Institutionen f&ouml;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| &lt; 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)
(&lt; (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) =&gt; 1.4142..
(my-sqrt 9) =&gt; 3.000..
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Institutionen f&ouml;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&gt;y
priority
(3+4)*5
3 + 4*5
prefix notation
sin(x)
print(x)
key words
go 10
if x&lt;y then 1 else 2
do ....
for ...
(+ 3 4)
(&gt; x y)
(defun sum-of-squares (x y)
(+ (square x) (square y)))
(* (+ 3 4) 5)
(+ 3 (* 4 5))
(square 5)
=&gt; 25
(sin x)
(print x)
(go 10)
(if (&lt; x y) 1 2)
(do ....
(for ...
actual parameters or argument
(sum-of-squares (+ 3 1) 6)
=&gt; 42 + 62 = 52
actual parameter evaluates and binds to
corresponding actual parameter
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Institutionen f&ouml;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)
=&gt; 7
=&gt; 25
=&gt; 5
=&gt; -3.15
=&gt; 24
=&gt; 5
=&gt; 10/7
=&gt; 12.5
=&gt; 7.0
(/ 10.0 7)
(float (/ 10 7))
=&gt; 1.42857...
=&gt; 1.42857...
(1+ 5)
(1- 5)
=&gt; 6
=&gt; 4
(max 2 5 3)
=&gt; 5
(truncate 10.7)
(round 10.7)
=&gt; 10
=&gt; 11
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Truth values
- nil
- all other values
usually t
false value
true value
Relational functions
(&lt; 5 10)
(&gt; 5 10)
(=10 (+ 5 5))
(&gt;= 5 5)
(/= 5 10)
=&gt; t
=&gt; nil
=&gt; t
=&gt; t
=&gt; t
Logical functions
(and (&gt; 10 5) (= 2 (- 4 2)))
(or (&gt; 10 5) (&lt; 10 5))
(not nil)
=&gt; t
=&gt; t
=&gt; t
Institutionen f&ouml;r datavetenskap
Anders Haraldsson
Conditional expression
x
if x&lt;=0
f(x,y) = x2+y if 0&lt;x&lt;10
x+y2 if x&gt;=10
all other cases
(defun f (x y)
(cond ((&lt;= x 0) x)
((and (&gt; x 0) (&lt; x 10))
(+ (* x x) y))
((&gt;= 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
((&lt; x 0) (- x))
(t x))
can be transformed to
(if (&lt; x 0) (- x) x)
(if predicate-expr expr expr)
An expression giving a truth-value is
called a predicate.
```