Functional Problem Solving (CSC 151 2014F) : EBoards

CSC151.01 2014F, Class 24: Recursion Basics


Overview

Preliminaries

Admin

Upcoming Work

Topics for Friday's Quiz

My quiz policies will continue through this week. You may bring a page of notes. You will not have a time limit, although I will encourage you to try to finish in ten minutes.

Cool Things Coming to Campus

Extra Credit Opportunities

Academic

Peer Support

Miscellaneous

Questions

Some challenges

Context:

The idea of recursion

Expressing recursion in Scheme - The problems

    ; Given a list, find out how many values are in the list
    (define my-length
      (lambda (lst)
         (if (null? lst)
             0
             (+ 1 (my-length (cdr lst))))))

How does this work?

    (my-length '(1 2 3 4))
    => (if (null? '(1 2 3 4))
           0
           (+ 1 (my-length (cdr '(1 2 3 4)))))
    => (if #f
           0
           (+ 1 (my-length (cdr '(1 2 3 4)))))
    => (+ 1 (my-length (cdr '(1 2 3 4))))
    => (+ 1 (my-length '(2 3 4)))
    => (+ 1 (if (null? '(2 3 4))
                0
                (+ 1 (my-length (cdr '(2 3 4))))))
    => (+ 1 (if #f
                0
                (+ 1 (my-length (cdr '(2 3 4))))))
    => (+ 1 (+ 1 (my-length (cdr '(2 3 4)))))
    => (+ 1 (+ 1 (my-length '(3 4))))
    => (+ 1 (+ 1 (if (null? '(3 4))
                     0
                     (+ 1 (my-length (cdr '(3 4)))))))
    ...
    => (+ 1 (+ 1 (+ 1 (my-length (cdr '(3 4))))))
    => (+ 1 (+ 1 (+ 1 (my-length '(4)))))
    => (+ 1 (+ 1 (+ 1 (if (null? '(4))
                          0
                          (+ 1 (my-length (cdr '(4))))))))
    ...
    => (+ 1 (+ 1 (+ 1 (+ 1 (my-length (cdr '(4)))))))
    => (+ 1 (+ 1 (+ 1 (+ 1 (my-length null)))))
    => (+ 1 (+ 1 (+ 1 (+ 1 (if (null? null)
                               0
                               (+ 1 (my-length (cdr null))))))
    => (+ 1 (+ 1 (+ 1 (+ 1 (if #t
                               0
                               (+ 1 (my-length (cdr null))))))
    => (+ 1 (+ 1 (+ 1 (+ 1 0))))
    => (+ 1 (+ 1 (+ 1 1)))
    => (+ 1 (+ 1 2))
    => (+ 1 3)
    => 4

    ; Given a list of integers, count even numbers in the list
    (define count-evens
      (lambda (lst)
        (cond
          [(null? lst) 0]
          [(even? (car lst)) (+ 1 (count-evens (cdr-list)))]
          [else (count-evens (cdr-list))])))

    ; Given a list of integers, extract the even values and
    ; give back a list of those values
    (define list-evens
      (lambda (lst)
        (cond
          [(null? lst) null]
          [(even? (car lst)) (cons (car lst) (list-evens (cdr-list)))]
          [else (list-evens (cdr-list))])))

Expressing recursion in Scheme - General Pattern

    (define PROC
      (lambda (lst)
        (if (null? lst)
            SOME-VALUE
            (COMBINE (car lst) (PROC (cdr lst))))))

    (define PROC
      (lambda (lst)
        (cond
          [(null lst?) SOME-VALUE]
          [(TEST (car lst?))
            (COMBINE (car lst) (PROC (cdr lst)))]
          [else
            ...])))

Lab