LISP Lect 4 I O HOF
LISP Lect 4 I O HOF
1
READING FROM A FILE & PRINT
File is opened as follows :
(setf fp(open “file.dat”: direction : input))
(read fp)
PRINT takes one argument except while writing to
a file.
(Print ‘ (A B C) )
(A B C)
(PRINT 5)
5
2
EXAMPLE : PRINT (Contd..)
(setf a 5)
(Print a)
(setf l ’(a b c))
(print l)
(a b c)
3
WRITING TO A FILE
Open a file for output as follows
(setf fp(open “file.dat” : direction : output))
(print ‘(1 2) fp)
Generally print is used before (read) as
(Print “type a number”)
(setf a (Read))
(print a)
4
CHARACTER STRINGS
Must be enclosed in quotation marks
>“strings are objects”
strings are objects
>(setf a “This is my book “)
> (stringp a )
T
a
“This is my book”
(print “computers are useful”)
computers are useful
5
The format function
Format function allows things to be written on
display or to a file
(format t “HELLO!”) ; t for terminal
~ % directive causes format to move to a new line
(format t “time and tide ~% wait for none”)
time and tide
wait for none
nil ; format returns a value of nil
~ a directive allows to print an argument
6
The format function (Contd..)
(setf p 2 q 3 )
(format t “~% p value is ~a ~% q value is ~a“ p q )
~ & directives tells format to move to a new line
unless it is already at the beginning of a new line.
This is useful when we don’t know where the
cursor is.
7
ITERATION ON NUMBERS AND LISTS
10
Dolist
11
Examples of Dolist
1. ;count the number of element in a list
(defun ce(l) ;ce count element
(let ( (te o) );te total elements in the list
(dolist (element te) )
(setf te (+ te 1 ) ) ) ) )
(te ‘(1 2 3 4 ) ) returns 4
(te ‘(a b c d e) ) returns 5
12
Examples of Dolist (Contd..)
2. ;find the positive sum ,negative sum and number of
zeros in a given list
(defun pnzsum(l): pnzsum – positive,nagative,number
of zeros
(let ((ps 0)
(ns 0
(nz 0 ) )
(dolist (element l)
(cond((zerop element )(+ nz 1) )
( plusp element )( + ps element ) )
(minusp element )(+ ns element) )
(t ‘(not a number )(return 0) ) ) ) )
(format t”~% nz ~a ps ~a ~ns ~a” nz ps ns) )
13
Exercises using Dolist
1. Develop a procedure that counts the number of
elements in the list that are below the freezing
point of water or above its boiling point
measured in Fahrenheit.
(fbpoint ‘(18 75 31 180 270 52))3
(defun fb point (l)
(setf freezing 32 boiling 212)
(let ((result 0 ))
(dolist (element l result)
(when (or (> element boiling )
(< element freezing ) )
(setf result (+ result 1) ) ) ) ) )
14
Exercises using Dolist (Contd..)
2. Write a procedure check – all – odd which accept a
list and returns T if all it’s elements are odd. If any
non odd element is found ,it returns nil and exits
from loop.
(defun check – all-odd ( L )
(dolist ( e L t )
(if (not (oddp e))(return nil ) ) ) ) )
;recursive version of check –all-odd
(defun check –all-odd (x)
(cond((null x ) t )
(t (unless (evenp (first x ) )
(check –all-odd (rest x) ) );unless); t );cond) ;defun
15
Exercises using Dolist (Contd..)
3. Write an iterative version of union of
two lists
(defun it –union (x y)
(let ( (result –set y))
(dolist (e x result –set )
(unless (member e y )(cons e result –set) ) ) ) )
16
Exercises using Dolist (Contd..)
4. Write an iterative version of intersection
of two sets
(defun it –intersection(x y)
(let ( (result–set nil ) )
(dolist (elemen t x result–set )
(when (member element y)
(cons element result –set) ) ) ) )
17
DO MACRO
• It is the most powerful iteration form in
LISP.
• It can bind any number of variables.
• It can step any number of index variables
any way you like.
• It allows to specify your own test to decide
when to exit the loop.
18
DO MACRO (Contd…)
The general form of DO is
(DO ((var1 init1[update 1])
(var2 init2[update 2])
.
:
(Var n init n [update n] ) )
(test action1 action2 …..action n) body)
19
DO MACRO (Contd..)
• Each variable is assigned it’s initial value
• If the test is evaluated to true, action 1, action 2,
…….action n are evaluated and the value of the
last one is returned; otherwise i.e. if the test is
evaluated to false, (then only) the forms in the
body of DO are evaluated .
• The body may contain RETURN which forces DO
to exit
• When DO reaches the end of the body, it begins
the next iteration of the loop with the value
updated by setting it to the value of its updated
expression.
• If the update expression is omitted, the variable
is left unchanged.
20
Example of DO MACRO
24
DO * MACRO
DO* macro is similar to DO except it creates and
updates the variables sequentially like let*, rather
than all at once like let.
Example: Find the largest element in the list using
Dolist and then using Do*
(defun find-largest (L)
(let (( largest ( first L)))
(dolist (element (rest L) largest )
(when (> element largest ) (setf largest element) ) ) ) )
25
LARGEST USING DO*
; largest using do* ; if updated form is not available
(defun find-largest (l) ; element will have the same
; previous computed value
(do* ((largest (first l)) ; In(element (first z) first z ) ) )
(z (rest L) (rest z))
(element (first z) (first z) ) ) ; initially z is rest of
;given list, updated
;value of z is rest of
;current list;
((null z ) largest) ; In parallel assignment z would have
been (rest L)
(when (> element largest) (setf largest element) ) ) )
; in sequential assignment z is updated to (rest z)
26
Example to demonstrate Do*
L = (5 12 20 15 4)
I Iteration
Do* largest (first L = 5)
Next time or in the next iteration, as no update value is there,
largest value will be same as that computed in the previous
iteration
(z is (12 20 15 4) and update of z is (20 15 4) as sequential
assignment is done in do*
(element is 12 and update is 20)
(null z) is not true so largest is not returned
12>5 so, largest is 12
II iteration proceeds with largest =12, z= (20 15 4) , element =20
20>12 so largest =20 and so on.
27
LOOP
• The general form is (loop <body>).
• The forms in the body are evaluated again
and again.
• When a (Return <expression>) is
encountered, the value becomes the value
of the loop.
• When (Return <expression>) is not
encountered, LOOP never stops.
28
Example: LOOP
29
LAMBDA EXPRESSIONS
• Labmda expressions are unnamed functions.
• Without using DEFUN and a name to the
procedure, we use LAMBDA.
• The general form of LAMBDA expression is
(LAMBDA (<formal-parameters>) <body>)
• Lambda is not a macro or a special function
that has to be evaluated.
• Rather it is a marker that says, “This list
represented a function”.
30
Examples of LAMBDA EXPRESSIONS
32
MAPCAR
• MAPCAR: This operator applies its first argument,
which is a function to the other arguments in a list.
• The type of arguments depends on the function.
• It returns the list of the results.
Note : Mapcar has two arguments one is the
procedure and the other is a list that consists of
arguments for which the procedure is to be
applied.
Hence the number of arguments may vary based
on the user.
The type of argument varies based on the function
33
Exemple of MAPCAR
(mapcar #’oddp ‘(1 2 3 4)) (T NIL T NIL)
#’ is used to produce a procedure object
from a procedure name.
(mapcar #’ atom ‘(1 (1 2) 3)) ( T NIL T)
(mapcar #’ = ‘(1 2 3) ‘(3 2 1)) (NIL T NIL)
34
Exemples of MAPCAR (Contd..)
(setf connect ‘((group1 seminar1)
(group2 seminar2)
(group3 seminar3))
(mapcar #‘ first connect); returns all the groups
(group1 group2 group3)
(mapcar #‘ second connect) ; returns all seminar
topics (seminar1 seminar2 seminar3)
35
REMOVE-IF and REMOVE-IF-NOT
36
Example of REMOVE-IF
(defun submittedp ( L )
(if (eql (cadr l) `Y) t nil ) )
(Remove – if #’ submittep ‘( ( Amit Y) (Anil n)
Ashish Y ) ) )
(Anil n )
37
Remove-if-not
Remove-if-not eliminates all elements that do not
satisfy a given predicate.
(remove-if-not #’ oddp ‘( 1 2 3 4 5 6 ) )
(1 3 5)
(remove-if-not #’ evenp ‘(125 2356 8798 9999 ) )
( 2356 8798 )
(remove-if-not #’ submittedp ‘( ( A Y ) ( B N) (C Y) ) )
( ( A Y ) (C Y ) )
38
COUNT-IF, COUNT-IF-NOT,FIND-IF
count-if counts the number of elements satisfying the
predicate.
(count-if-not #’ oddp ‘(121 567 824 ) )
2
(count-if-not #’ submittedp ‘( ( A Y ) ( B N) (C Y) )
2
find-if finds the first match found
(find-if #’ oddp ‘(1234 5678 8877 ) )
8877
(find-if #1 oddp ‘(2 4 6 8 ) )
Nil
(find-if #1 sybmittedp ‘( ( A N ) ( B N) (C N) ) ( D Y ) ) )
(D Y )
39
FUNCALL and APPLY
FUNCALL and APPLY also take procedure arguments.
FUNCALL applies its first argument value to its other
argument’s values.
The template for FUNCALL is
(funcall #’ <procedure specification>
<argument 1>…<argument n>)
(funcall #’ first ‘(e1 e2 e3)) e1
(funcall #’ append ‘(a b) ‘(x y)) (a b x y)
(funcall #’ + 3 4) 7
(mapcar #’ + ‘(3) ‘(4)) 7 ;mapcar first applies + to 3 and 4
To apply + to other arguments there are no elements after 3 and after
4
(funcall #’ (LAMBDA (X Y) (+ (* X X) (* Y Y))) 3 4) 25
40
FUNCALL (Contd..)
• The lambda expression is generated by
evaluating the first parameter of funcall.
The generated lambda expression is used
with other arguments.
• NOTE: programs are treated as data in
LISP.
41
APPLY
APPLY: Apply uses its first argument’s
value on the elements of its second
argument’s value, which must be a list.
(apply #’ <procedure name> <list of
arguments>)
(apply #’ first ‘((e1 e2 e2)) e1
(apply #’ + ‘( 3 4)) 7
(apply #’ (LAMBDA (x y) (+ (* x x) (* y y)))
‘(3 4)) 25
42
IMPORTANCE OF FUNCALL & APPLY
• FUNCALL and APPLY applies function
values to its arguments.
• These are also useful when we want to
use a procedure whose name is the
value of a parameter.
• Difference between Funcall and Apply
is that Apply requires all the other
arguments except first in a list.
43
Example of FUNCALL
Example:
(defun revr(argument procedure) (FUNCALL
procedure argument))
(revr ‘(as you like it) #’ cdr) (you like it)
revr is a user defined function that has two
parameters in which second one is a
procedure.
In the body of revr the procedure parameter is
used to work as a procedure with the
argument using FUNCALL
44
Difference between MAPCAR & FUNCALL
45
PROPERTY LISTS
• Properties can be attached to symbols to
store relationships
• Properties can be stored or defined using
PUTPROP or SETF and GET as follows
• (PUTPROP name attribute property) like
• (PUTPROP ‘lion ‘legs 4)
• (SETF (GET <symbol> (<attrib>) value) as
• (SETF (GET ‘lion ‘legs) 4)
46
Property Lists (contd..)
• To remove the property use REMPROP as
• (REMPROP ‘lion ‘legs 4)
• To retrieve the value use GET
(GET <symbol> < attrib>) as
(GET ‘lion ‘legs) returns 4
Example:
(PUTPROP ‘Rama ‘Kousalya ‘Mother)
(GET ‘Rama ‘Kousalya) returns Mother
47
Property Lists (contd..)
• (get ‘Mary ‘age) returns NIL
• (setf (get ‘Mary ‘age) 45) returns 45
• (get ‘Mary ‘age) returns 45
• (setf (get ‘Mary ‘job) teacher)
• (get ‘Mary ‘job) returns teacher
(setf (get ‘Mary ‘children) ‘(Kamal Kishore)) (get
‘Mary ‘children)
returns (Kamal Kishore)
• (symbol-plist ‘Mary) returns all properties as
(children (Kamal Kishore) job teacher age 45 )
48
Water-Jug problem in LISP
(defun waterjug(a b c)
; a capacity of jug A
; b capacity of jug B
; c Required amount in A or B
(cond (( and (> c a) (> c b)) ‘Not feasible)
( ( not (zerop (rem c (our-gcd a b) ) ) ) ); GCD of a and b
is not divisible by c, no feasible solution)
(T (water-make 0 0 A B C ) ) ) )
(defun our-gcd (u v)
(cond ((zerop u) v)
(t (our-gcd (rem v u) u) ) ) )
49
Water-Jug problem in LISP contd..
50
Water-Jug problem in LISP contd..
( (= y 0) (cons '(Fill B)
(water-make x b a b c ) ) )
( ( > (- a x) y) ; required amount to fill A is > y
(cons '(Empty B into A)
(water-make (+ x y) 0 a b c ) ) )
(T (cons '(Fill A from B) ;required amount is poured from B
(water-make a ( - y (- a x)) a b
c) ) ) ) )
51
BFS in LISP
; breadth-first search
; Input the graph
(defun mainbfs()
(setf (get 's 'neighbors) '(a d)
(get 'a 'neighbors) '(s b d)
(get 'b 'neighbors) '(a c e)
(get 'c 'neighbors) '(b)
(get 'd 'neighbors) '(s a e)
(get 'e 'neighbors) '(b d f)
(get 'f 'neighbors) '(e) )
;(breadth-first ‘s ‘f)
;(breadth-first ‘s ‘e) )
52
BFS in LISP contd..
;this function finds paths from a node
(defun extend (path); to make a new queue
(print (reverse path) )
(mapcar #' (lambda (new-node) (cons
new-node path) )
;form new paths
(remove-if #'(lambda (neighbor)
(member neighbor path) )
(get (first path) 'neighbors) ) ) )
53
BFS in LISP contd..
(defun breadth-first (start finish &optional (queue
(list (list start) ) ) )
(cond ((endp queue) nil )
( (eq finish (first (first queue) ) )
(reverse (first queue) ) )
(t (breadth-first
start
finish
(append (rest queue)
(extend (first queue) ) ) ) ) ) )
54
DFS IN LSP
; depth-first search
55
DFS IN LSP (Contd..)
(defun extend (path)
(print (reverse path ) )
(mapcar #' (lambda (new-node) (cons
new-node path ) )
;form new paths
(remove-if #'(lambda (neighbor)
(member neighbor path ) )
(get (first path) 'neighbors ) ) )
56
DFS IN LSP
(defun depth-first (start finish &optional (queue (list
(list start ) ) ) )
(cond ((endp queue) nil)
( (eq finish (first (first queue ) ) )
(reverse (first queue ) ) )
(t (depth-first
start
finish
(append (extend (first queue ) )
(rest queue ) ) ) ) ) )
57
BEST-FIRST SEARCH IN LSP
; best-first search
58
BEST-FIRST SEARCH IN LSP (Contd..)
59
BEST-FIRST SEARCH IN LSP (Contd..)
60
BEST-FIRST SEARCH IN LSP
62