Hello
I've noticed a discrepancy between the interpreter and the compiler: in both of these cases:
(define (run n) (letrec ((x (+ n 1)) (y (+ x x))) (display y) (newline)))
(define (run n) (define x (+ n 1)) (define y (+ x x)) (display y) (newline))
(run 1) outputs 4 when compiled. When interpreted, it gives a "number expected, (+ #!unbound #!unbound)" exception.
(Thanks to Rickdangerous on irc for the example.)
Christian.
Afficher les réponses par date
On 30-Sep-05, at 6:02 PM, Christian wrote:
Hello
I've noticed a discrepancy between the interpreter and the compiler: in both of these cases:
(define (run n) (letrec ((x (+ n 1)) (y (+ x x))) (display y) (newline)))
(define (run n) (define x (+ n 1)) (define y (+ x x)) (display y) (newline))
In Scheme the above code is illegal. However, the compiler is more permissive than the interpreter so it does not detect the error (it sorts the bindings according to the variable dependencies so that the binding on y is performed after binding x). Moral: use the interpreter to debug your code before you compile it!
Marc
At 18:12 Uhr -0400 30.09.2005, Marc Feeley wrote:
In Scheme the above code is illegal.
Ok, I also realize that the interpreter is not trying to access an outer binding in this case, as this code shows which gives the same results as before (#!unbound in the interpreter) (I imagine it works by assignment and uses #!unbound as default value).
(define run #f) (let ((x -10)) (set! run (lambda (n) (define x (+ n 1)) (define y (+ x x)) (display y) (newline))))
(I read that R6RS will add letrec*, and specify internal define in terms of it. So I guess you will change the interpreter later to use letrec* in this case.)
Christian.