Functional Problem Solving (CSC 151 2016S) : EBoards

CSC151.02 2016S, Class 25: Recursion Basics


Overview

Preliminaries

Admin

Reminders

Upcoming Work:

Extra Credit

Academic / Artistic

Peer

Regular Peer

Misc

Far in the Future

No Extra Credit, But Still Good

Questions

Background

The primary components of an algorithm

The idea of recursion

or

    (define count
      (lambda (lst)
        (if (null? lst)
            0
            (increment (count (cdr lst))))))

Goal: Find alphabetically first book by title

Assistant

..

Revised code

(define alphabetically-first
  (lambda (lst)
    ; If there's only one element
    (if (null? (cdr lst))
        ; That element is alphabetically first
        (car lst)
        ; Take the rest of the list
        ; Find the alphabetically first
        ; Take the first of the two
        (let ([mine (car lst)]
              [guess (alphabetically-first (cdr lst))])
          (if (string<=? guess mine)
              guess
              mine)))))

Some sample recursive procedures

Expressing recursion in Scheme