6.001 Recitation #2 – Feb 7,
2003
RI: Konrad Tollmar,
http://www.ai..mit.edu/~konrad/6001/
The MIT Scheme programming environment
Basic Scheme
-
primitives /
combinations
-
abstractions
-
lambda
-
special form cond
/ if
1. Evaluate these expressions
(+ 2 3)
(+ (* (/ 12 2) 1000) 1)
(+ 4)
(- 3)
(/ 5)
(/ 60 5 2 3)
(+)
(*)
(-)
(define a 1)
(define a+1 (+ a 1))
a
a+1
(define a 2)
a+1
(define four 4)
four
(+ four 1)
(+ (four) 1)
A. Evaluate these
lambda procedures
(define six (lambda () 6))
six
(+ six 1)
(+ (six) 1)
B. Define the lambda
procedures twice and second.
(define
twice (…))
(twice 2)
==> 4
(twice 3) ==> 6
(define
second … )
(second 2 15
3) ==> 15
(second 34 -5 16) ==> -5
(define plus +)
(define add
(lambda (x y) (+
x y)))
(define abs …)
Define two procedures average and square. Use these procedures to crate a procedure avaragesquares that (surprise;-) average squares, e.g.
(avaragesquares 4 2)
è
10
Define a procedure that converts US Dollars to Swedish kronor.
(define
RATE 0.105)
(convert
“USD” 10)
è
95
SEK
(convert
“SEK” 100)
è
10.5
SEK
Hint:
(string=? "PIE" "PIE")
=> #t
Create some nicer output:
(convert "USD" 10)
USD->SEK: 95.23809523809524
;Value: 95.23809523809524
Hint:
string-append string ...
number->string number
display “string”