Midterm #1 solutions
What will Scheme print?
(let ((a 3) (b 4))
(lambda () (+ a b)))
The body of the LET is (lambda () (+ a b)).
The value of that
expression is a procedure -- after substitution, it's the same
procedure as (lambda () 7).
But it's not the number 7, which was
the most common wrong answer.
If we named this procedure P, then
the expression (P) would mean to invoke P, and *that* would have the
(let ((a 3) (b 4))
((lambda () (* a b))))
It's 12, not 7, because this one uses *, not +.
But the important
thing is that those extra parentheses invoke the procedure, just like
the (P) discussion above.
So the value isn't a procedure this time.
Some people said 7 for the first example and procedure for this one;
we haven't figured out what they were thinking.
(every - (filter number? '(the 1 after 909)))
The FILTER part of the expression has the value (1 909), selecting the
numbers from the given sentence.
(By the way, not everyone recognized
this as the name of a Beatles song, from the album _Let It Be_, the
last one they released, but the next-to-last one they recorded.)
A few people thought this was a data abstraction violation, because
FILTER returns a list, not a sentence, and EVERY definitely wants a
sentence as argument.
I guess this could be confusing, but I defined
in lecture during week 2 a FILTER that works on sentences, and that's
the one we meant here.
(every - '(1 909)) computes (- 1) and (- 909), putting the results in
Some people thought this was an error because - requires
two arguments, but it doesn't.
With one argument it means negation,
rather than subtraction.
(Look it up in the Scheme reference manual
at the back of your course reader!)
A few people said -908, because they thought EVERY would call the
function once, giving both numbers as arguments.
That would be
(APPLY - '(1 909)), not EVERY.
Scoring for the above:
One point each, all or nothing.
(cons '(a b) (list '(c d) 'e))