Presentation is loading. Please wait.

Presentation is loading. Please wait.

Common Lisp! John Paxton Montana State University Summer 2003.

Similar presentations


Presentation on theme: "Common Lisp! John Paxton Montana State University Summer 2003."— Presentation transcript:

1 Common Lisp! John Paxton Montana State University Summer 2003

2 Montana Facts State Animal: Grizzly Bear

3 Montana Facts State Flag

4 Recursion Tail recursion occurs if the final answer is known when the base case is hit. Most Lisp compilers can detect tail recursion and can translate the tail recursion into an equivalent looping structure.

5 Rest Recursion (defun my-reverse (alist) (if (null alist) nil (append (my-reverse (rest alist)) (list (first alist))) )

6 Rest Recursion > (my-reverse '(1 2 3)) (3 2 1) > (trace my-reverse)

7 Rest Recursion > (my-reverse '(1 2 3)) 1. Trace: (MY-REVERSE '(1 2 3)) 2. Trace: (MY-REVERSE '(2 3)) 3. Trace: (MY-REVERSE '(3)) 4. Trace: (MY-REVERSE 'NIL) 4. Trace: MY-REVERSE ==> NIL 3. Trace: MY-REVERSE ==> (3) 2. Trace: MY-REVERSE ==> (3 2) 1. Trace: MY-REVERSE ==> (3 2 1) (3 2 1)

8 Tail Recursive Version (defun my-reverse (alist &optional (result nil)) (if (null alist) result (my-reverse (rest alist) (cons (first alist) result) )

9 Tail Recursive Version > (my-reverse '(1 2 3)) (3 2 1) > (trace my-reverse) > (my-reverse ‘(1 2 3))

10 Tail Recursive Version 1. Trace: (MY-REVERSE '(1 2 3)) 2. Trace: (MY-REVERSE '(2 3) '(1)) 3. Trace: (MY-REVERSE '(3) '(2 1)) 4. Trace: (MY-REVERSE 'NIL '(3 2 1)) 4. Trace: MY-REVERSE ==> (3 2 1) 3. Trace: MY-REVERSE ==> (3 2 1) 2. Trace: MY-REVERSE ==> (3 2 1) 1. Trace: MY-REVERSE ==> (3 2 1) (3 2 1)

11 First/Rest Recursion (defun reverse-all (alist) (cond ((null alist) nil) ((atom alist) alist) (t (append (reverse-all (rest alist)) (list (reverse-all (first alist))))) )

12 First/Rest Recursion > (reverse-all '(((1) (2 3) 4))) ((4 (3 2) (1)))

13 Question 1.Trace (reverse-all ‘(1 ((2) 3))) by hand.

14 Optional Parameters > (defun add (n &optional (m 1)) (+ n m)) ADD > (add 10) 11 > (add 10 2) 12

15 Rest Parameters (defun example (&rest elements) (length elements) ) EXAMPLE > (example) > (example 1 2 3 4) > (example ‘(1 2))

16 Keyword Parameters (defun add (&key (n1 0) (n2 0)) (+ n1 n2)) ADD > (add) > (add :n1 7 :n2 3) > (add :n2 3)

17 Questions 1.Develop a recursive function called my- count that computes the number of times than an atomic key appears as a top- level element in a list. For example, (my- count 1 ‘(1 2 (1 3) 1)) should return a 2. Trace the function. 2.Repeat the above question but now make the function tail recursive. Trace the function.

18 Questions 3.Change the function my-count so that it counts all occurrences of the key. For example, (my-count 1 ‘(1 2 (1 3) 1)) should return a 3. Trace the function. 4.Define my-nthcdr, a function that uses a keyword parameter with a default value of 1. Do not use nthcdr in the function body!


Download ppt "Common Lisp! John Paxton Montana State University Summer 2003."

Similar presentations


Ads by Google