Skip to main content

CSC 151.01, Class 32: Naming Local Procedures

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • PSA
    • Questions
  • Quiz.
  • Quick review.
  • Lab.

News / Etc.

  • Continue partners

Rotating reminders

  • Have a great break!

Upcoming Work

  • No lab writeup!
  • No homework over break (other than the reading).
  • Reading for Monday after break: Turtle Graphics

Extra credit (Academic/Artistic)

Extra credit (Peer)

  • Singers Concerts during spring break.
    • If there is an admission fee, I will reimburse up to $20.
  • Ultimate games during spring break.
    • There should be no admission fee.
  • Baseball games during spring break (Arkansas & Alabama).
    • There should be no admission fee.
  • Baseball games last weekend of spring break. (1 hour suffices; no more than two units for the weekend.)

Extra credit (Misc)

Good things to do

  • Read books.
  • Talk with friends.
  • Enjoy nature.
  • Get sleep.
  • Exercise.
  • Make a positive difference to someone else.

Pre-break PSA

  • You are awesome people. Take care of yourselves.
  • One can recover from the intensity of half of a Grinnell semester in many ways. You need not ingest substances. You need not engage in carnal acts. If you choose to ingest substances, please do so in moderation. If you do the latter, remember that consent is necessary, and that you should respect your own and your partner’s body.

Questions

Will you make everyone stand up and say their name since I didn’t
have a chance to study for the quiz?
yes.

Quiz

  • Yay! Fun.

Quick review of reading

  • We like to have helper recursive procedures.
  • Good design suggests that others should not be able to access those procedures; they should be local.
  • let and let* don’t work for recursive procedures. We need something else.
  • letrec: Just like let, except that it allows you to write recursive local procedures
(letrec ([name (lambda (val) (... (name val) ...))])
  ...)
  • “Named let” is a very strange way of defining recursive procedures, that eventually becomes natural. We are thinking ahead to calling it. Suppose we decide that our implementation of iota (which we will call yoda) needs a helper with two parameters, m and lst. We want m to start at n, and lst to start as null.
(define yoda
  (lambda (n)
    (let helper ([m n]
                 [lst null])
      (write (list 'helper m lst)) (newline)
      (if (zero? m)
          lst
          (helper (- m 1) (cons (- m 1) lst))))))

Lab

Note: For letrec, the syntax is

(letrec ([kernel (lambda (PARAMS) BODY)])
  (kernel INIT-VAL INIT-VAL)) 

For named let, the syntax is

(let kernel ([PARAM INIT-VAL]
             [PARAM INIT-VAL])
   BODY)

Note: There are helpful all and any procedures.

  • (all pred? lst) checks if the predicate holds for all the values in the list.
  • (any pred? lst) checks if the predicate holds for any of the values in the list.

Note: Your husk should generally be responsible for verifying the preconditions.