6.001 Recitation #2 – Sept 6, 2002
RI: Konrad Tollmar, NE43-V608
konrad@ai.mit.edu,
http://www.ai..mit.edu/~konrad/6001
The MIT Scheme programming environment
Basic Scheme
-
primitives
-
combinations
-
abstractions
-
lambda
-
special form cond
-
special form if
1. Evaluate these expressions
(+ 2 3)
-> 5
(+ (* (/ 12 2) 1000) 1)
->
6001
(+ 4)
->
4
(- 3)
->
-3
(/ 5)
->
0.2
(/ 60 5 2 3)
->
2
(+)
->
0
(*)
->
1
(-)
->
error
(define a 1)
->
undef
(define a+1 (+ a 1))
->
undef
a
->
1
a+1
->
2
(define a 2)
->
undef
a+1
->
2
(define four 4)
->
undef
four
->
4
(+ four 1)
->
5
(+ (four) 1)
->
error
A. Evaluate these
lambda procedures
(define six (lambda () 6))
->
undef
six
->
6
(+ six 1)
->
error
(+ (six) 1)
->
7
(define inc
(lambda (x) (+ x 1)))
->
undef
B. Define the lambda
procedures twice and second.
(* 2
x))
(b))
Plus take any number of arguments, add take only two arguments
(define abs (lambda (x)
(cons ((> x 0) x)
((< x 0) (- x))
(else 0)))
(define abs (lambda (x)
(if (< x 0) (- x) x)))
(define avarage (lambda (x y) (/ (+ x y) 2)))
(define square (lambda (x) (* x x)))
(define avsq (lambda (x y) (avarage (square x)
(square y))))
(define convert (lambda (currency amount)
(cond ((string=?
currency "USD") (/ amount RATE))
((string=?
currency "SEK") (* amount RATE))
(else
"error"))))
(define convert (lambda (currency amount)
(cond ((string=?
currency "USD")
(begin
(display (string-append "USD->SEK:
" (number->string (/ amount RATE))))
(/ amount RATE)))
((string=?
currency "SEK") (* amount RATE))
(else
"error"))))