Reputation: 391
I've been reading through chapter 5 of sicp and have gotten stuck on a piece of code--namely, the assembler presented in 5.2. This is what it looks like:
(define (extract-labels text receive)
(if (null? text)
(receive '() '())
(extract-labels (cdr text)
(lambda (insts labels)
(let ((next-inst (car text)))
(if (symbol? next-inst)
(receive insts
(cons (make-label-entry next-inst insts)
labels))
(receive (cons (make-instruction next-inst) insts)
lables)))))))
Isn't the lambda only going to be called when the text is null? So how can we ask for the car of 'text' then?
EDIT
Thanks for the answers, but I'm still not seeing it. If the text is not null, doesn't extract-lables call itself recursively, until the text is null? In which case how can we call the car of it?
Upvotes: 3
Views: 442
Reputation: 179422
if
takes up to three arguments: a condition, a if-true
(then) expression, and an if-false
(else) expression. The indentation is all weird, so it looks wrong.
It should appear as
(define (extract-labels text receive)
(if (null? text)
(receive '() '())
(extract-labels (cdr text)
(lambda (insts labels)
(let ((next-inst (car text)))
(if (symbol? next-inst)
(receive insts
(cons (make-label-entry next-inst insts)
labels))
(receive (cons (make-instruction next-inst) insts)
labels)))))))
Observe that the result of the expression is (receive '() '())
if text
is null and (extract-labels ...)
otherwise.
Upvotes: -1
Reputation: 64700
The lambda gets the appropriate scope: at the time the lambda is defined text
is not null, so calling car text
works fine.
Upvotes: 1