CS 330 Lecture 32 – Scheme, Part II


  • why functional?
  • why not functional?
  • reverse engineering
  • finish merge
  • program this
  • double, square, cosine
  • higher-order functions
  • map and filter
  • variable-length argument lists
  • symbolic differentiation


Reverse Engineering

Program This

Choose one or both:

  • Write contains, which takes two arguments: a list and an atom. Return true if the list contains the atom.
  • Write nth-element, which returns the the nth-element (starting at 0) of the list.



(define clog
  (lambda (n base)
      ((<= n 1) 0)
      (else (+ 1 (clog (/ n base) base))))))


(define calc-tax
  (lambda (price) (* 1.055 price)))

(calc-tax 3.69)
;; ((lambda (price) (* 1.055 price)) 3.69)

(define !
  (lambda (n) 
      ((= n 0) 1)
      (else (* n (! (- n 1)))))))

(define first car)
(define rest cdr)

(define merge
  (lambda (a b)
      ((null? a) b)
      ((null? b) a)

      ; there's something in both a and b

      ; a wins
      ((> (first b) (first a)) (cons (first a) (merge (rest a) b)))

      ; b wins or tie
      (else (cons (first b) (merge a (rest b)))))))


Scheme got rid of VOID.
VOID checked out library books.
Never returned them.


Leave a Reply

Your email address will not be published. Required fields are marked *