Functional Problem Solving (CSC 151 2014F) : Outlines

Outline 42: Higher-Order Procedures, Revisited


Held: Monday, 17 November 2014

Back to Outline 41 - Randomized (Unpredictable) Drawing. On to Outline 43 - Analyzing Procedures.

Summary

We revisit the topic of higher-order procedures, one of the most important techniques in languages like Scheme. Higher-order procedures are procedures -- like map, left-section, or compose -- that take other procedures as parameters, return other procedures as values, or both.

Related Pages

Overview

Administrivia

Upcoming Work

Extra Credit Opportunities

Academic

Peer Support

Miscellaneous

Project Notes

Background: Guiding Principles

Background: The Value of Repetition

The following is variant of something my colleague John Stone says ...

You learn from reading.

You learn from writing.

So ... extract the common code so you don't have to write it again.

Procedures as First-Class Values

(define apply-to-2-and-3
  (lambda (proc)
    (proc 2 3)))
(define adder
  (lambda (n)
    (lambda (x)
      (+ x n))))
(define inc (adder 1))

Old Notes

The following are notes I wrote for past versions of the course. I probably won't discuss any/all in class.

Two Motivating Examples

Procedures as Parameters

Procedures as Return Values

(define redder
  (lambda (amt)
    (lambda (color)
      (rgb ...))))
(define left-section
  (lambda (func left)
    (lambda (right)
      (func left right))))
(define l-s left-section)
(define right-section
  (lambda (func right)
    (lambda (left)
      (func left right))))
(define r-s right-section)

Encapsulating Control

(define map
  (lamda (fun lst)
     (if (null? lst)
         null
         (cons (fun (car lst))
               (map fun (cdr lst))))))
(define all-numbers?
  (lambda (lst)
    (or (null? lst)
        (and (pair? lst)
             (number? (car lst))
             (all-numbers? (cdr lst))))))
(define all-symbols?
  (lambda (lst)
    (or (null? lst)
        (and (pair? lst)
             (symbol? (car lst))
             (all-symbols? (cdr lst))))))
(define all
  (lambda (test? lst)
    (or (null? lst)
        (and (pair? lst)
             (test? (car lst))
             (all test? (cdr lst))))))

Concluding Comments