Docsity

Recursion and Functional Programming in COMP 211, Spring 2009 - Prof. Walid M. Taha, Assignments of Computer Science

Partial preview of the text

Download Recursion and Functional Programming in COMP 211, Spring 2009 - Prof. Walid M. Taha and more Assignments Computer Science in PDF only on Docsity!

1

Generative (Non-structural)

Recursion

Corky Cartwright Department of Computer Science Rice University

The Recipe Until Now

  • Data analysis and design including generic templates
  • For each function in the design (visible interface)
    • Contract, purpose
    • Examples (stated as tests)
    • Template Instantiation
      • Precisely followed the structure of the data we consume
      • Using this template, we can do "almost everything”
    • Testing

Non-structural Functional Programs

  • Best explained by presenting some examples before discussing the general template. Problem: efficiently sort a list of numbers Good solutions: merge-sort, quick-sort

Merge Sort

  • Not going to present the actual program because it will be an exercise on the next homework assignment.
  • Idea:
    • Base case: list of length 0 or 1
    • Inductive case: split the list into two (almost) equal parts sort each part merge the two results Why non-structural?

Quicksort Breaks Structural Template

(define (qsort l) (cond [(empty? l) empty] [else (local ((define pivot (first l)) (define other (rest l))) (append (qsort [filter (lambda (x) (<= x pivot)) other]) (list pivot) (qsort [filter (lambda (x) (> x pivot)) other])))]))

Quicksort Still Terminates

(define (qsort l) (cond [(empty? l) empty] [else (local ((define pivot (first l)) (define other (rest l))) (append (qsort [filter (lambda (x) (<= x pivot)) other]) (list pivot) (qsort [filter (lambda (x) (> x pivot)) other])))])) Why?

A More General Recipe

  • Data analysis and design
  • Contract, purpose, header
  • Examples
  • Template Instantiation
    • A bit more flexible than before (non-structural)
  • Explicit termination argument
  • Testing

Generative Template

(define (generative-recursive-fun problem) (cond [(trivially-solvable? problem) (determine-solution problem)] [else (combine-solutions ... problem ... (generative-recursive-fun (generate-problem-1 problem)) … (generative-recursive-fun (generate-problem-n problem)))]))

General framework for proving termination

  • Devise a metric (a size function) with some familiar structural type as the output (usually nat) for the problem and show that each recursive call involves a smaller problem than the original one.
  • In pathological cases, this ordering may require the use of lexicographic ordering on n - tuples (or unbounded sequences) of data values. These pathologies are rare in practice. Not a single occurrence in DrJava code base.

Why Generative Recursion?

  • What if we can choose between
    • a structural solution and
    • a generative solution?
  • Often, the second is much faster
    • Sorting
    • Simpler example from book: greatest-common-divisor (GCD) gcd(6,9)=3, gcd (99, 18) = 9, etc. structural version so brain-damaged I could not follow the narrative. I had to infer what the code did. Rant: local function in book often have no contracts!
    • Even better example: searching an ordered list (but not functional!)

Some Algorithm Families

  • Sorting and Searching
  • Mathematical iteration: bisection, Newton's method.
  • Backtracking (traversing a maze, 8 queens)
  • Dynamic Programming (with Java)

Termination Argument

; If we start with an interval S wide, then ; we only need a limited number of steps ; to reach an interval R wide. In particular, ; the intervals will proceed as S, S/2, S/4, ; ..., and will reach size smaller than R in ; log 2 (R/S) steps.

For Next Class

  • Homework due Monday
  • Continue Reading:
    • Ch 25-28: Non-structural recursion.
  • Start on next homework assignment
    • (mergesort lon) (Problem 26.1.2 but top- down rather than bottom-up version of mergesort