如何在Scheme中运行时重载函数?
RT。 我想在运行时重新定义一个函数,以便我可以在运行时更改系统的行为。 谢谢。
rt.
I want to redefine a function at run time so that i can change the behavior of the system at run time.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能建议使用 let 在本地执行此操作,这也适用于这种意义上的关键字:
或者甚至将它们重新定义为常量,记住,Scheme 是 lisp-1:
或者甚至:
仅在本地执行此操作可以保留函数式风格。
It might be advisable to use let to do this locally, this can also apply to keywords in this sense:
Or even redefine them to constants, remember, Scheme is a lisp-1:
Or even:
Doing it only locally preserves the functional style.
假设您想要重载之前定义的函数,只需重新定义它即可。这也适用于重新定义函数,例如 car 和 cdr,例如将 car 变成 cdr:
但是,我认为您将无法通过这样的重新定义影响其他已定义的函数,因此使用 car 的系统函数仍将使用原来的系统车而不是你的:
1
我猜这样做的原因是,一旦读取函数或函数,符号就会在内部消失评估并且符号被它们所绑定的内容替换;在本例中,是函数的实际代码。因此,将符号重新绑定到不同的函数不会影响已定义的代码的其余部分。这通常是一件好事,因为它有助于维护引用透明度。
如果您想重新定义诸如 lambda 或 cond 之类的方案关键字,请使用 let-syntax (请参阅 http: //community.schemewiki.org/?scheme-faq-language)
Assuming you want to overload a function you defined earlier, simply define it again. This also works for redefining functions such as car and cdr, e.g. to make car into cdr:
However, I think you won't be able to affect other already defined functions with such a redefinition, so a system function which uses car will still use the original system car and not yours:
1
I guess the reason for this is that internally the symbols disappear once a function gets read or evaluated and the symbols are replaced by what they're bound to; in this case, the actual code of the function. So rebinding a symbol to a different function won't affect the rest of your already defined code. This is usually a good thing because it helps uphold referential transparency.
If you want to redefine scheme keywords such as lambda or cond, use let-syntax (see http://community.schemewiki.org/?scheme-faq-language)