Skip to main content

Class 20: Anonymous Procedures and Local Bindings

Held: Friday, 24 February 2017

We revisit procedures in Scheme. We consider a variety of techniques for writing *anonymous procedures, procedures that do not have explicit names.*

Preliminaries

Overview

  • What is a procedure?
  • Describing procedures
  • Anonymous procedures
  • Anonymous procedures through lambda expressions
  • Other kinds of anonymous procedures

Updates

News / Etc.

  • Continue partners!
  • Both drawings and values and Monday’s topic benefit from drawing diagrams, so I brought you graph paper.
  • A mentor told some of you that I was serious, not angry, when discussing my academic honesty concerns. She was wrong. Although I was serious, I am also angry. I am not angry at anyone; we had a clear misunderstanding. I’m just angry that it happened.
  • I apologize for forgetting to tell you to do the reading on local bindings.

Rotating reminders

  • Ask questions via email! I’m always happy to (try to) answer questions via email. There is no need to apologize when sending me questions. If I take too long to answer, send another email (or even text, if it’s a reasonable hour).
  • Visit our store! We have office supplies in the commons. Price is free will donation.

Upcoming Work

Extra credit (Academic/Artistic)

  • Screenagers documentary, 10 am, Saturday, Strand
  • Met Opera Live in HD showing of Rusalka, Saturday, Feb. 25. Talk 11:30-noon, opera noon-3pmish.
  • CS Table, Tuesday at noon: ???
  • W. Kamau Bell, Tuesday at 6pm, Harris.
  • Prazak Quartet, Tuesday, 28 February 2017, 7:30 p.m., Herrick.
  • CS Extras, Thursday at 4:15 pm, Sam’s research

Extra credit (Peer)

  • Ritalin Test squad, 2-4pm Saturday, Bucksbaum, The Wall
  • Jazz band concert Friday at 7:30 p.m. in Sebring-Lewis.

Extra credit (Misc)

  • Host a prospective student NEXT WEEKEND. (March 5, April 8, April 23)

Good things to do

  • Diving championships this weekend (2pm today, noon Saturday)

Friday PSA

  • Take care of yourself.
  • Take care of others.

What is a procedure?

  • One of the basic building blocks of programming.
  • Essentially, an encapsulated algorithm
    • Takes some inputs
    • Generates some outputs

Describing Procedures

Traditionally, we write something like

(define *name-of-procedure*
  (lambda (*parameters*)
    *body*))

When applying a procedure, we effectively substitute the arguments we call the procedure on for the parameters in the body, and then evaluate the body.

Anonymous Procedures

  • Procedures without names.
  • Useful when we just need a procedure once
    • Saves the need to define it (code is more concise)
    • Saves the need to document it
    • Makes code work on its own
  • In Scheme, whereever you write a procedure name, you can also write the corresponding lambda expression.
  • Very useful with map
  • Examples
  • ((lambda (x y) (* (+ x y) (- x y))) 2 3)
  • (map (lambda (x) (* 5 (+ 1 x))) (iota 10))

Other ways to build anonymous procedures

  • Sectioning
  • Composition

Lab

  • Do the lab.
  • Be prepared to reflect.