球拍方案常数的重新定义

发布于 2024-10-12 04:10:36 字数 502 浏览 8 评论 0原文

我目前正在使用球拍方案和 DrRacket 作为一些探索性编程的编辑器。当我尝试重新定义一个函数时,我收到以下消息:

define-values: cannot re-define a constant: png->byte-list

现在我曾经接受这是该语言的属性,并且对此感到沮丧,但还是接受了它。阅读如何在中重新定义常量标识符DrScheme? 我可以看到我不一定要接受这个限制。

我的问题如下:

  1. 这个功能只有R5RS语言才有吗?有没有办法为任意语言关闭此功能?
  2. 或者,如果我切换 R5RS 语言,这会对我使用目前正在使用的模块产生影响:racket/gui/base 和 web-server/insta?

I am currently using racket scheme with DrRacket as the editor for some exploratory programming. When I try and redefine a function I get the following message:

define-values: cannot re-define a constant: png->byte-list

Now I used to accept that this was a property of the language and was frustrated by it but kind of accepted it. Having read How can you re-define a constant identifier in DrScheme? I can see that I don't necessarily have to accept this limitation.

My questions are as follows:

  1. Is this feature only available in the R5RS language? Is there a way to switch this feature off for an arbitrary language?
  2. Alternatively if I switch the R5RS language will this have an impact on me using the modules I'm using at the moment: racket/gui/base and web-server/insta?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-10-19 04:10:36

如果您在定义窗口中进行此类重新定义,则可以使用#langracket进行此类重新定义。需要记住的是,这不是一种突变(这在 R5RS 实现中很常见),例如:

#lang racket
(define orig-+ +)
(define + whatever)

不会有 orig-+ 绑定到真正的加法函数。当这个模块被编译时,Racket(静态地)知道+是你定义的东西,所以对它的任何引用都是对你自己的定义的引用——所以上面的代码将绑定orig- ++ 的“未初始化”值。如果您确实想做类似的事情,那么您可以使用不同的名称来要求原始的 +

#lang racket
(require (rename-in racket [+ orig-+]))
(define + whatever)

最重要的是,编译模块时会发生内联 - 这是一个不同的问题。 Racket 通常希望内联引用代码中从未发生变化的绑定,因此如果您尝试最后一段代码,您将无法在 REPL 上进一步重新定义 +。您可以通过两种方式之一来处理这个问题 - 第一种是显式改变绑定:

#lang racket
(require (rename-in racket [+ orig-+]))
(define + #f)
(set! + whatever)

这使得编译器避免任何内联。另一个选项是 DrRacket 标志,它打开避免所有内联的编译器标志。请注意,这样做的速度损失可能会很大。

Such re-definitions are possible with #lang racket if you do them in the definitions window. The thing to keep in mind is that this is not a kind of mutation (which is common with R5RS implementations), for example, this:

#lang racket
(define orig-+ +)
(define + whatever)

will not have orig-+ bound to the real addition function. When this module is compiled, Racket knows (statically) that + is something that you define, so any references to it are references to your own definition -- so the above code will bind orig-+ to the "uninitialized" value of +. If you really want to do something like that, then you can require the original + under a different name:

#lang racket
(require (rename-in racket [+ orig-+]))
(define + whatever)

On top of that, there's inlining that happens when the module is compiled -- which is a different issue. Racket will usually want to inline references to bindings that are never mutated in the code, so if you try that last piece of code you will not be able to further redefine + on the REPL. You can deal with that in one of two ways -- the first is to mutate the binding explicitly:

#lang racket
(require (rename-in racket [+ orig-+]))
(define + #f)
(set! + whatever)

which makes the compiler avoid any inlining. Another option is that DrRacket flag, which turns on a compiler flag that avoids all inlining. Note that the speed penalty for this can be significant.

莫相离 2024-10-19 04:10:36

1 - 我不是Scheme专业人士,但我只是尝试重新定义乘法函数(*)。
它不适用于“禁止重新定义初始绑定”。但如果未选中 - 它会起作用:

欢迎使用 DrRacket,版本 5.0.2 [3m]。
语言:R5RS [自定义];内存限制:128 MB。
> (* 2 2)
4
> (定义 (* ab) a)
> (* 2 2)
2
>

但是,我无法收到您的错误(定义值:无法重新定义常量:...)
如果我重新定义现有函数,我会得到 (define-values: 无法更改常量变量:...)

2 - 我认为如果模块具有显式指定语言的 #lang 指令,那么它不应该成为问题(我猜)。

1 - I'm no pro in Scheme, but I just tried to redefine multiplication function (* ).
It doesn't work with "Disallow redefinition of initial bindings". But if unchecked - it works:

Welcome to DrRacket, version 5.0.2 [3m].
Language: R5RS [custom]; memory limit: 128 MB.
> (* 2 2)
4
> (define (* a b) a)
> (* 2 2)
2
>

However, I was not able to get your error (define-values: cannot re-define a constant:...)
If i redefine existing function i get (define-values: cannot change constant variable:...)

2 - I would think that if modules have #lang directive that specifies explicitly the language it should not be a problem (I guess).

太阳男子 2024-10-19 04:10:36

下面是如何定义乘法

(defineMultiply*);名为“multiply”且值为 * 的变量

Here is how to define multiplication

(define multiply *) ; variable named "multiply" with a value of *

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文