66-2210-01 Programming in Lisp - PowerPoint PPT Presentation

About This Presentation
Title:

66-2210-01 Programming in Lisp

Description:

Tower of Hanoi. Three pegs, S(start), T(temp), E(end) N disks ... (if ( n 1) (hanoi-aux (- n 1) start temp end) ... (if ( n 1) (hanoi (- n 1) temp end start) ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 22
Provided by: csR4
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: 66-2210-01 Programming in Lisp


1
66-2210-01 Programming in Lisp
  • Recursion, Data Abstraction, Mapping, Iteration

2
Example Both-ends
  • Define a procedure that gives both ends of a list
  • gt (setf itinerary (Albany NYC Chicago Seattle
    Anchorage))
  • gt (both-ends itinerary)
  • (ALBANY ANCHORAGE)
  • Three steps
  • Get first element
  • gt (first itinerary)
  • ALBANY
  • Get last element
  • gt (first (last itinerary))
  • ANCHORAGE
  • Combine the two
  • gt (list (first itinerary) (first (last
    itinerary)))
  • Define procedure
  • gt (defun both-ends (l) (list (first l) (first
    (last l))))

3
Error handling
  • Both-ends with error handling
  • (defun both-ends (l)
  • (if (listp l)
  • (case (length l)
  • (0 NIL)
  • (1 (list (first l) (first l)))
  • (t (list (first l) (first (last l)))))
  • NIL))

4
DoTimes
  • DOTIMES is Lisps way of doing iteration
  • C/C
  • for (i0 iltn i)
  • ltbodygt
  • Lisp
  • (dotimes (i n) ltbodygt)
  • First parameter is a list with three elements
  • counter variable (e.g. i)
  • number of times to iterate (e.g. n)
  • Counter variable (i) ranges from 0 to n-1
  • Optional return value can be specified (default
    is NIL)
  • (dotimes (i n return_value) ltbodygt)

5
Factorial
  • Definition of Factorial
  • C Implementation
  • int factorial (int x)
  • int i,f
  • f 1
  • for (i1 iltx i) f f i
  • return f
  • Lisp Implementation
  • (defun factorial (n)
  • (let ((f 1))
  • (dotimes (i n)
  • (setf f ( f ( i 1))))
  • f
  • )
  • )
  • Tip Compute factorial(100) using C/C and Lisp

6
Recursively Calculating Factorial
  • Mathematical Definition of Factorial
  • C/C Implementation
  • long Factorial (long X)
  • if (X lt 1)
  • return 1
  • else
  • return X Factorial(X-1)
  • Lisp Implementation
  • (defun recursive-factorial (x)
  • (if (lt x 1)
  • 1
  • ( x (recursive-factorial (- x 1)))))

7
Recursive calls
  • Show recursion when calling (recursive-factorial
    4)
  • Begin (recursive-factorial 4)
  • Since 4gt1, evaluate 4 (recursive-factorial
    3)
  • Begin (recursive-factorial 3)
  • Since 3gt1, evaluate 3
    (recursive-factorial 2)
  • Begin (recursive-factorial 2)
  • Since 2gt1, evaluate
    2(recursive-factorial 1)
  • Begin (recursive-factorial 1)
  • Since 1lt1, return 1
  • End (recursive-factorial 1),
    returns 1
  • 2 (recursive-factorial 1) 2 1
    2
  • End (recursive-factorial 2), returns 2
  • 3 (recursive-factorial 2) 3 2 6
  • End (recursive-factorial 3), returns 6
  • 4 (recursive-factorial 3) 4 6 24
  • End (recursive-factorial 4), returns 24

8
Fibonacci Numbers
  • Mathematical Definition of Fibonacci Numbers
  • Sequence
  • X 0 1 2 3 4 5 6 7 8
  • Fib(X) 0 1 1 2 3 5 8 13 21
  • Recursive Solution
  • (defun fib (x)
  • (cond (( x 0) 0)
  • (( x 1) 1)
  • (t ( (fib (- x 1)) (fib (- x 2))))))

9
Fib(6) Function calls
10
Recursive Definition of Length
  • Length
  • (defun mylength (l)
  • (if (endp l)
  • 0
  • ( 1 (mylength (rest l)))))
  • Alternate definition
  • (defun mylength2 (l)
  • (mylength2-aux l 0))
  • (defun mylength2-aux (l count)
  • (if (endp l)
  • count
  • (mylength2-aux l ( count 1))))
  • Note
  • All recursive calls simply return the final value
    evaluated.
  • No additional computations

11
Tail Recursion
  • Tail Recursion
  • The final expression of a function is a recursive
    call
  • No additional computations are done to that
    expression
  • That is, return value is JUST the result of the
    recursive call
  • Lisp handles tail recursion efficiently
  • Mylength is not tail recursive
  • Mylength2 is tail recursive
  • Example
  • Write a function to produce N atoms with value
    A.
  • gt (produce-list-of-a 5) Example call
  • (A A A A A)

12
Produce-list-of-a
  • Using dotimes
  • (defun produce-list-of-a (n)
  • (let ((la NIL))
  • (dotimes (i n la) (push 'A la))))
  • Recursion, not tail recursive
  • (defun produce-list-of-a (n)
  • (if ( n 0)
  • nil
  • (cons 'A (produce-list-of-a (- n 1)))))
  • Recursion, with tail recursion
  • (defun produce-list-of-a (n)
  • (produce-list-of-a-aux n NIL))
  • (defun produce-list-of-a-aux (n list-so-far)
  • (if ( n 0)
  • list-so-far
  • (produce-list-of-a-aux
  • (- n 1)
  • (cons 'A list-so-far))))

13
Count-Atoms
  • Write function to count number of atoms in an
    expr
  • (sqrt ( (expt x 2) (expt y 2)))
  • has eight atoms
  • (defun count-atoms (l)
  • (cond ((null l) 0)
  • ((atom l) 1)
  • (t ( (count-atoms (first l))
  • (count-atoms (rest l))))))

14
Tower of Hanoi
  • Three pegs, S(start), T(temp), E(end)
  • N disks
  • Goal Move disks from peg S to peg E
  • Restriction Larger disk cant be placed on top
    of smaller disk
  • S T E

15
Tower of Hanoi
  • Solution to Tower of Hanoi
  • (defun hanoi-aux (n start end temp)
  • (if (gt n 1) (hanoi-aux (- n 1) start temp
    end))
  • (print (list start end))
  • (if (gt n 1) (hanoi-aux (- n 1) temp end
    start)))
  • (defun hanoi (n) (hanoi-aux n 'S 'E 'T))
  • Example Runs
  • gt (hanoi 2)
  • (S T)
  • (S E)
  • (T E)
  • NIL

gt (hanoi 3) (S E) (S T) (E T) (S E) (T S) (T
E) (S E) NIL
16
Optional
  • Produce-list-of-a function(s)
  • (defun produce-list-of-a (n)
  • (produce-list-of-a-aux n NIL))
  • (defun produce-list-of-a-aux (n list-so-far)
  • (if ( n 0)
  • list-so-far
  • (produce-list-of-a-aux
  • (- n 1)
  • (cons 'A list-so-far))))
  • Redefined with optional parameters
  • (defun produce-list-of-a (n optional
    list-so-far)
  • (if ( n 0)
  • list-so-far
  • (produce-list-of-a
  • (- n 1)
  • (cons 'A list-so-far))))
  • Note optional values are bound to NIL, by default

17
Optional Parameters (cont)
  • Solution to Hanoi
  • (defun hanoi-aux (n start end temp)
  • (if (gt n 1) (hanoi-aux (- n 1) start temp
    end))
  • (print (list start end))
  • (if (gt n 1) (hanoi-aux (- n 1) temp end
    start)))
  • (defun hanoi (n) (hanoi-aux n 'S 'E 'T))
  • Revised with optional parameters
  • (defun hanoi (n optional (start 'S) (end 'E)
    (temp 'T))
  • (if (gt n 1) (hanoi (- n 1) start temp end))
  • (print (list start end))
  • (if (gt n 1) (hanoi (- n 1) temp end start)))
  • Note notice the syntax for initializing optional
    parameters

18
Rest
  • Rest - Specifies a variable number of arguments
  • Example Assume only accepts 2 arguments.
    Define Plus, a function that adds an arbitrary
    number of arguments)
  • gt (plus 2 3)
  • gt (plus 2 3 4 5 8)
  • Solution
  • (defun plus (arg1 rest other_args)
  • (plus-aux arg1 other_args))
  • (defun plus-aux (arg1 other_args)
  • (if (null other_args)
  • arg1
  • (plus-aux ( arg1 (first other_args))
  • (rest other_args))))

19
Key Parameters
  • KEYword parameter
  • Useful when function has MANY parameters
  • Most are tied to default values
  • Examples
  • gt (rotate-list '(a b c d e)) rotate one
    element right
  • (E A B C D)
  • gt (rotate-list '(a b c d e) direction 'left)
  • (B C D E A)
  • gt (rotate-list '(a b c d e) distance 2)
  • (D E A B C)
  • gt (rotate-list '(a b c d e) direction 'left
    distance 2)
  • (C D E A B)

20
Key
  • Use key to define Key parameters
  • (defun rotate-list (l key (direction 'right)
    (distance 1))
  • (if (eq direction 'left)
  • (rotate-list-left l distance)
  • (rotate-list-right l distance)))
  • (defun rotate-list-right (l n)
  • (if (zerop n)
  • l
  • (rotate-list-right (append (last l)
    (butlast l))
  • (- n 1))))
  • (defun rotate-list-left (l n)
  • (if (zerop n)
  • l
  • (rotate-list-right (append (rest l) (list
    (first l)))
  • (- n 1))))

21
Aux
  • Aux keyword is used as a shorthand for Let
  • Produce-list-of-a using Let
  • (defun produce-list-of-a (n)
  • (let ((la NIL))
  • (dotimes (i n la) (push 'A la))))
  • Using Aux
  • (defun produce-list-of-a (n aux (la NIL))
  • (dotimes (i n la) (push 'A la)))
Write a Comment
User Comments (0)
About PowerShow.com