CS 330 Lecture 33 – Higher-order
Agenda
- program this
- double, square, cosine
- higher-order functions
- map and filter
- variable-length argument lists
- symbolic differentiation
TODO
- Introduction Google’s MapReduce: http://code.google.com/edu/parallel/mapreduce-tutorial.html.
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.
Code
hof.ss
(define first car) (define rest cdr) (define contains (lambda (l item) (cond ((null? l) #f) ((= (first l) item) #t) (else (contains (rest l) item))))) (define nth-element (lambda (l index) (cond ((null? l) '()) ((= index 0) (first l)) (else (nth-element (rest l) (- index 1)))))) (define double (lambda (l) (cond ((null? l) '()) (else (cons (* 2 (first l)) (double (rest l))))))) (define lsin (lambda (l) (cond ((null? l) '()) (else (cons (sin (first l)) (lsin (rest l))))))) (define square (lambda (l) (cond ((null? l) '()) (else (cons (* (first l) (first l)) (square (rest l))))))) (define mymap (lambda (f l) (cond ((null? l) '()) (else (cons (f (first l)) (mymap f (rest l))))))) (define differentiate (lambda (f) (cond ((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)
Haiku
Gamification.
Dress up blah with achievements.
Like matching parens.
Dress up blah with achievements.
Like matching parens.