CS 330 Lecture 32 – Scheme, Part II

Agenda

  • 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

TODO

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.

Code

clog.ss

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

stuff.scm

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

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

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

(define first car)
(define rest cdr)

(define merge
  (lambda (a b)
    (cond
      ((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)))))))

Haiku

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

Comments

Leave a Reply

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