In §5.2.1, the following function is presented for creating a register
(define (make-register name)
(let ((contents '*unassigned*))
(define (dispatch message)
(cond ((eq? message 'get) contents)
((eq? message 'set)
(lambda (value) (set! contents value)))
(else
(error "Unknown request -- REGISTER" message))))
dispatch))
However, the parameter nome is nowhere used in the body, so it's basically swallowed and ignored by the function. Therefore, what's the relevance of it?
Indeed, it's later used like this
(define (make-new-machine)
(let ((pc (make-register 'pc))
(flag (make-register 'flag))
(stack (make-stack))
; ...
but would there be any difference if we had omitted that name paramter in the first place and used make-register like this?
(define (make-new-machine)
(let ((pc (make-register))
(flag (make-register))
(stack (make-stack))
; ...
or am I missing something?
I'd say no, it wouldn't make any difference.
Scheme might have some complex behaviour (the one used here being one, for people used to most other language: functions carry their context, and "stack" is not linear. So even tho
make-registercall, and more over theletinside it, are finished, since the returneddispatchstill refers to the local context built by thatlet, so, that instance of variablecontentscontinues to exist), it is also quite "pure". There isn't any mysterious side effects. So, your reasoning seems correct: thatnameserves no purpose.My bet is that this is related to the exercises following in the same page: reader are asked to modify
make-registerto trace modifications of the register (display name of the register, old value and new value). And for that, of course, you need the name of the register to be at handLike this
Demo:
So, my code just added the
(if trace...)line, plus the messages to changetrace. But the name of the register was already there. I bet this is because this kind of modification was expected that thatnameis there. Or, more pragmatically, that whoever wrote those exercise, as any teacher would do, wrote first the complete solution, and the remove some part to create the partial code given to students, and forgot to remove thename:D