0% found this document useful (0 votes)
48 views

LISP Lect 4 I O HOF

1. The document discusses various looping constructs in Lisp like DOTIMES, DOLIST, DO and DO* for iterating over numbers and lists. It provides examples to iterate over a list and perform operations on each element. 2. The FORMAT function allows printing to the terminal or a file with formatting directives. Examples show using FORMAT to print values and strings with newlines. 3. Character strings must be enclosed in quotation marks. Functions like STRINGP, PRINT, SETF can be used to work with strings.

Uploaded by

manit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

LISP Lect 4 I O HOF

1. The document discusses various looping constructs in Lisp like DOTIMES, DOLIST, DO and DO* for iterating over numbers and lists. It provides examples to iterate over a list and perform operations on each element. 2. The FORMAT function allows printing to the terminal or a file with formatting directives. Examples show using FORMAT to print values and strings with newlines. 3. Character strings must be enclosed in quotation marks. Functions like STRINGP, PRINT, SETF can be used to work with strings.

Uploaded by

manit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 62

READ AND PRINT FOR I / O

• Read takes no arguments except when


reading from a file.
• (Read ) is a function when called, evaluation
is suspected until user types in a valid s-
expression
eg. (Read)
Ram Ram
(Cons(Read) ‘(Two Three))
(One Two Three)

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

• Dotimes Dolist Do Loop


• The general form of DOTIMES is
(dotimes(<count parameters > <
upperbound form> [ <result form> ] <body>)
• The upper bound form is evaluated and
produces a number n;
• Numbers are assigned 1 to n -1 one after
another to count parameter.
• Thus the body is executed n times.
8
EXAMPLE : DOTIMES
Example ; Computation of mn given m & n
;m*m*m*………m(n times)
(defun mpowern(m n)
(let ((result 1))
(dotimes(count n result)
(setf result (* m result ) ) ) ) )
result = m
= m.m
= m.m.m
= m.m…m
n times
9
EXAMPLE 2 : DOTIMES

;factorial of n using dotimes


(defun factorial (n)
(let ((fact 1) )
(dotimes( i n fact)
(setf fact ( * (+ i 1 ) fact ) ) ) ) )

10
Dolist

Dolist is similar to dotimes except that the


elements of a list are assigned one after
another to the parameter.
The general form of dolist is
( dolist ( <element parameter> < list form>
[< result form> ] < body >)

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

1. Write a function launch which counts down


numbers from 10 to 1 and prints “Blast Off“ when
zero is encountered.
(defun launch(n)
(do((cnt n ( - cnt 1 ) ) )
((zerop cnt) ’Blast ‘Off)
(print cnt) ) )
Input (launch 10)
Output 10 9 8 7 6 5 4 3 2 1 Blast Off
21
Examples of DO MACRO
2. Write check-all-odd using Do
;Returns T if all elements
;in the list are odd
;Returns NIL even if one element is even
(defun check-all-odd(x)
(do((z x (rest z) ) ) ; z is x initially
( (null z) t) ; z is (rest z) later
(if (evenp (first z) ) (return nil) ) ) )
22
Examples of DO MACRO (Contd…)

3. Write Do factorial using Do


(defun factorial (n)
(Do ( ( result 1 (* n result) )
(n n ( - n 1) ) )
((zerop n) result) ) )
Input : (factorial 5 )
Output : 120
Valued of result : 1 5 20 60 120
23
Advantage of Do over Dotimes
and Dolist
• It can step the index variables any way
you like so, it can count down instead of
up.
• Do can bind multiple variables
• In dotimes and Dolist, there is a
surrounding LET and an explicit SETF
required.

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

;counts the number of times the symbol


appears in a list
(setf cheers ‘(cheer cheer cheer ) )
(setf loop-count 0)
loop (when (endp cheers) (return loop-count) )
(setf cheers (rest cheers) )
(setf loop-count (+ loop-count 1) ) )

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

Example of Lambda expressions


1. (lambda (n) (* n n)) is equivalent to
(defun sq (n) (* n n))
(lambda 4) or (sq 4)  16
2. (lambda (x y) (+ (* x x) (* y y)) 2 3)  16
Observe that in the above example the
arguments are provided along with the
definition.
Lambda expressions are used in higher
order functions.
31
HIGHER ORDER FUNCTIONS
• Functions that take a function as their
argument or return a function as their
result are called higher order functions.

• LISP functions such as MAPCAR,


REMOVE-IF, REMOVE-IF-NOT, COUNT-
IF take functions as arguments.

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)

(defun square (n) (* n n))


(mapcar #’square ‘(1 2 3 4 5))  (1 4 9 16 25)

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

REMOVE-IF and REMOVE-IF-NOT simplify


filtering operations.
Remove-if eliminates all elements that do
satisfy a given predicate.
(Remove-if #’ oddp ‘(1 2 3))  (2)
(Remove-if #’ evenp ‘(1 2 3 4 5 6)) (1 3 5)

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

• MAPCAR applies function to each of the


arguments in a list separately.
• MAPCAR applies its first argument i.e. a function
many times; each time an argument is taken
from the list.
• Funcall applies it first argument which is a
function only once.
• The arguments to function are the arguments
given in Funcall excluding the first one.
• The number of arguments in funcall depends on
the function.

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..

; rem(u v) gives remainder by dividing u by v


(defun water-make(x y a b c)
; Main function to implement allowed operations
using Production rules
(cond ( (= x c) '( Required amount in A ) )
( (= y c) ‘ (Required amount in B ) )
( (= x a) (cons '(Empty A)
(water-make 0 y a b c ) ) )

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

(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) )

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

(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) )

58
BEST-FIRST SEARCH IN LSP (Contd..)

(setf (get 's 'coordinates) '(0 3)


(get 'a 'coordinates) '(4 6)
(get 'b 'coordinates) '(7 6)
(get 'c 'coordinates) '(11 6)
(get 'd 'coordinates) '(3 0)
(get 'e 'coordinates) '(6 0)
(get 'f 'coordinates) '(11 3) )

59
BEST-FIRST SEARCH IN LSP (Contd..)

(defun straight-line-distance (node-1 node-2)


(let ( ( coordinates-1 (get node-1 'coordinates ) )
(coordinates-2 (get node-2 'coordinates ) ) )
(sqrt (+ (expt (- (first coordinates-1)
(first coordinates-2 ) )
2)
(expt ( - (second coordinates-1 )
(second coordinates-2 ) )
2)))))

60
BEST-FIRST SEARCH IN LSP

(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 ) ) ) )
61
HILLCLIMBING IN LISP
hill climbing search

(defun hill-climb (start finish &optional (queue (list (list start ) ) ) )


(cond ((endp queue) nil) ; Queue empty ?
((eq finish (first (first queue ) ) ) ; finish found ?
(reverse (first queue ) ) )
(t (hill-climb
start
finish
(append (sort (extend (first queue ) )
#'(lambda (p1 p2)
(closerp p1 p2 finish ) ) )
(rest queue ) ) ) ) ) )

62

You might also like