Functional Problem Solving (CSC 151 2015F) : Outlines

Outline 24: Recursion Basics


Held: Wednesday, 7 October 2015

Back to Outline 23 - Revisiting Lists. On to Outline 25 - Recursion Basics, Continued.

Summary

We begin our exploration of recursion, the most general form of repetition available in Scheme. You can use recursion to both build and iterate over different kinds of values.

Related Pages

Overview

Administrivia

Upcoming Work

Extra Credit Opportunities

Academic

Peer Support

Miscellaneous

How would you like to read a fanfic made by one of the most prominent friendly AI researchers in the entire world? And how would you like to learn about existential risk, rationality, and effective altruism through the medium of HARRY POTTER? (included at bottom of the email)

Check out the first chapter, and come to the first meeting (10/10) of Harry Potter and the Methods of Rationality club! Saturdays, 3pm in the CS commons.

We're hosting a hackathon for Spring 2016 that involves teaching people about the HPMOR fanfic through the medium of apps and websites.

http://hpmor.com/chapter/1

Also, like the FB page to get updates. https://www.facebook.com/HPMORHackathon?fref=ts

Contact [wuruth17] to get more information.

Other Good Things

Repetition

Some Challenges

Recursion

Recursion in Scheme

Remember that I tend to think in abstractions first. But we'll look at some concrete forms, too.

Here's the form of a typical recursive procedure:

(define PROC
  (lambda (VAL)
    (if (BASE-CASE-TEST)
        (COMPUTE-BASE-CASE VAL)
        (COMBINE (EXTRACT-DATA VAL)
                 (PROC (SIMPLIFY VAL))))))

When the value you're working with is a list and your base case is the null list, the form is somewhat simpler:

(define PROC
  (lambda (LST)
    (if (null? LST)
        NULL-CASE
        (COMBINE (EXTRACT-DATA (car LST))
                 (PROC (cdr LST))))))

Sometimes it's useful to grab the recursive result first, particularly if you're going to use it in multiple ways.

(define PROC
  (lambda (LST)
    (if (null? LST)
        NULL-CASE
        (let ((recursive-result (PROC (cdr LST))))
          (if (TEST)
              (COMBINE-1 (EXTRACT-DATA-1 (car LST)) recursive-result)
              (COMBINE-2 (EXTRACT_DATA-2 (car LST)) recursive-result))))))

Lab