6.001 Recitation – Oct 2,
2002
RI: Konrad
Tollmar
• Pop Quiz
0. ‘(say your name)
1. Warm-up
(define (three) 3)
;Value: "three -->
#[compound-procedure 2 three]"
three
;Value: #[compound-procedure 2 three]
(three)
;Value: 3
(define four 4)
;Value: "four --> 4"
four
;Value: 4
(four)
;The object 4 is not applicable.
(+ (three) four)
;Value: 7
(define (add-x x)
(lambda (y) (+ y x)))
;Value: "add-x -->
#[compound-procedure 3 add-x]"
(define add-10 (add-x 10))
;Value: "add-10 -->
#[compound-procedure 4]"
(add-10 5)
;Value: 15
2. Procedures
Given the procedure,
(define
apply-to-3-and-4
(lambda (p) (p 3 4)))
What choices for <exp> in a combination of the form (apply-to-3-and-4 <exp>) will cause the following numbers to be returned?
a. 7 - (apply-to-3-and-4 +)
b. 1 -
(apply-to-3-and-4 (lambda (x y) (- y x))), or
(apply-to-3-and-4 (lambda (x y) 1) ;-)
c. 2 -
(apply-to-3-and-4 (lambda (x y) (/ y 2)))
d. 3 -
(apply-to-3-and-4 (lambda (x y) x))
e. 4 -
(apply-to-3-and-4 (lambda (x y) y))
3. Recursive procedures
Someone, by misstake i guess, redefined the standard + and – procedures (among many other procedures). Write a new recursive add procedures by only using the =, inc and dec procedures.
(define (add a b)
(if (= a 0)
b
(inc (add (dec
a) b))))
4. List procedure
Write a process version of a procedure called remove that takes a number and a list and returns a new version of the list where that number is no longer an element.
(define (remove elm lst)
(if (null? lst)
nil
(if (= elm (car lst))
(remove elm (cdr lst))
(cons (car lst) (remove elm (cdr lst))))))
or,
(define (remove elm lst)(filter (lambda (x) (not (= x elm))) lst))