Racket v5.1.1 中的范围界定错误?
有人请告诉我这是 Racket (v5.1.1) 中的一个错误,
它似乎是一个范围问题。
(参见下面的代码和输出)
example-1 的返回表明 x 不是 按应有的方式增加 在示例 2 和示例 3 中。
函数 example-2 只是 example-1 的副本 从 doit 中删除反向指令。
函数 example-3 (如您所见)是一个副本 也一样,但有一个附加参数,它本身 不会改变任何东西,但是当我测试它的值时 cond 语句显示 x 正在递增。
另外,如果我没有从 example-1 返回任何内容 而是打印它现在返回的内容,它将 x 显示为 正在递增。
(define (example-1 lst)
(letrec([x 0]
[doit (lambda ()
(reverse
(foldl
(lambda (v store)
(set! x (add1 x))
(cons v store))
'()
lst)))])
(let*([results (doit)])
(list x results)
)))
(define (example-2 lst)
(letrec([x 0]
[doit (lambda ()
(foldl
(lambda (v store)
(set! x (add1 x))
(cons v store))
'()
lst))])
(let*([results (doit)])
(list x results))))
(define (example-3 lst id)
(letrec([x 0]
[doit (lambda ()
(reverse
(foldl
(lambda (v store)
(set! x (add1 x))
(cons v store))
'()
lst)))])
(let*([results (doit)])
(cond [(= 1 id) 'junk])
(list x results)
)))
(printf "example-1 : ~a~n" (example-1 '(a b c)))
(printf "example-2 : ~a~n" (example-2 '(a b c)))
(printf "example-3 : ~a~n" (example-3 '(a b c) 1))
输出:
example-1 : (0 (a b c))
example-2 : (3 (c b a))
example-3 : (3 (a b c))
Somebody please tell me this is a bug in Racket (v5.1.1),
It seams to be a scoping problem.
(see code and output below)
The return from example-1 shows that x is not
getting incremented as it should and as it does
in example-2 and example-3.
The function example-2 is just a copy of example-1
with the reverse instruction removed from doit.
The function example-3 (as you can see) is a copy
as well but has an additional parameter which by itself
does not change anything but when I test it's value in
the cond statement it shows x as being incremented.
In addition if I don't return anything from example-1
but rather print what it now returns it shows x as
being incremented.
(define (example-1 lst)
(letrec([x 0]
[doit (lambda ()
(reverse
(foldl
(lambda (v store)
(set! x (add1 x))
(cons v store))
'()
lst)))])
(let*([results (doit)])
(list x results)
)))
(define (example-2 lst)
(letrec([x 0]
[doit (lambda ()
(foldl
(lambda (v store)
(set! x (add1 x))
(cons v store))
'()
lst))])
(let*([results (doit)])
(list x results))))
(define (example-3 lst id)
(letrec([x 0]
[doit (lambda ()
(reverse
(foldl
(lambda (v store)
(set! x (add1 x))
(cons v store))
'()
lst)))])
(let*([results (doit)])
(cond [(= 1 id) 'junk])
(list x results)
)))
(printf "example-1 : ~a~n" (example-1 '(a b c)))
(printf "example-2 : ~a~n" (example-2 '(a b c)))
(printf "example-3 : ~a~n" (example-3 '(a b c) 1))
output:
example-1 : (0 (a b c))
example-2 : (3 (c b a))
example-3 : (3 (a b c))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误 - 我用更简单的版本提交了你的代码。 (尽管它与 jit 无关。)
编辑:该错误已已修复< /a> 现在。
It's a bug -- I filed it with a simpler version of your code. (It's unrelated to the jit though.)
Edit: the bug is fixed now.