CS 330 Lecture 33 – Higher-order


  • program this
  • double, square, cosine
  • higher-order functions
  • map and filter
  • variable-length argument lists
  • symbolic differentiation


Program This

Choose one:

  • 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 first car)
(define rest cdr)

(define contains
  (lambda (l item)
      ((null? l) #f)
      ((= (first l) item) #t)
      (else (contains (rest l) item)))))

(define nth-element
  (lambda (l index)
      ((null? l) '())
      ((= index 0) (first l))
      (else (nth-element (rest l) (- index 1))))))

(define double
  (lambda (l)
      ((null? l) '())
      (else (cons (* 2 (first l)) (double (rest l)))))))

(define lsin
  (lambda (l)
      ((null? l) '())
      (else (cons (sin (first l)) (lsin (rest l)))))))

(define square
  (lambda (l)
      ((null? l) '())
      (else (cons (* (first l) (first l)) (square (rest l)))))))

(define mymap
  (lambda (f l)
      ((null? l) '())
      (else (cons (f (first l)) (mymap f (rest l)))))))

(define differentiate
  (lambda (f)
      ((number? f) 0)
      ((equal? 'x f) 1)
      ((equal? '+ (first f))
       (list '+ 
             (differentiate (nth-element f 1))
             (differentiate (nth-element f 2))))
      ((equal? '* (first f))
       (list '+
             (list '* (nth-element f 1) (differentiate (nth-element f 2)))
             (list '* (nth-element f 2) (differentiate (nth-element f 1)))))
      (else (display 'Idunno)))))

;(define (contains l item)


Dress up blah with achievements.
Like matching parens.


Leave a Reply

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