Class 5: Breaking the Functional Paradigm

Slides

Display version (pdf).

This class will be held in the location of our labs, MI 302.

Handouts

Homework

Submit your solutions to the following exercises electronically in a file called ex.scm contained in a folder called class05.

  1. Go back and read the last slide from Class 4 (DISPLAY version) that we didn't get to during lecture.
    Then rewrite the following two functions using lambda:
    (define (foo x)
      (* x 12))
    
    (define (bar x)
      (let ((temp (sqrt x)))
        (* temp (+ temp 3))))
    
  2. Write a function print-sum that takes a list of numbers and prints the numbers with plus signs between.
    For example:
    > (print-sum '(1 2 3))
    1 + 2 + 3
    > (print-sum '(5))
    5
    
    The function should return void.
  3. Using a let closure, a returned lambda, and repeated set! calls, create a function factory make-summer that returns a procedure which takes a single number, adds it to a running sum, and returns that running sum. For example:
    > (define summer (make-summer))
    > (summer 4)
    4
    > (summer 10)
    14
    > (summer 10)
    24
    
    Hint: start with the code for make-counter in the slides.
  4. (OPTIONAL) Create a function factory make-better-summer that works like make-summer, except that the returned procedure displays the entire sum at each step, and returns void.
    For example:
    > (define bs (make-better-summer))
    > (bs 10)
    10 = 10
    > (bs 4)
    4 + 10 = 14
    > (bs 8)
    8 + 4 + 10 = 22
    > (bs 16)
    16 + 8 + 4 + 10 = 38