1 / 18

Common Lisp!

Common Lisp!. John Paxton Montana State University Summer 2003. Montana Facts. State Animal: Grizzly Bear. Montana Facts. State Flag. Recursion.

roch
Download Presentation

Common Lisp!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • 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 • 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. • Repeat the above question but now make the function tail recursive. Trace the function.

  18. Questions • 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. • Define my-nthcdr, a function that uses a keyword parameter with a default value of 1. Do not use nthcdr in the function body!

More Related