Functional Problem Solving (CSC 151 2016S) : Outlines

Outline 43: Higher-Order Procedures, Revisited


Held: Wednesday, 20 April 2016

Back to Outline 42 - Trees. On to Outline 44 - Files in Scheme.

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

Reminders

Upcoming Work:

Extra Credit

Academic / Artistic

Peer

Regular Peer

Misc

Not so Far in the Future

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