Scheme中定义表达式返回的值 [英] Value returned by a define expression in Scheme

查看:109
本文介绍了Scheme中定义表达式返回的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MIT/GNU计划中运行过此程序:

I ran this in MIT/GNU Scheme:

(define x (+ 2 3))

译员随后打印:

;Value: x

但是根据我的教科书,由定义表达式返回的值应该是未定义的.为什么解释器随后打印; Value:x"?

But according to my textbook, the value returned by a define expression should be undefined. Why did the interpreter print ";Value: x" then?

推荐答案

如果标准报告未指定返回值或未定义返回值,则实现实际上可以自由选择返回的值,这将符合标准.这也意味着您不能依赖一个实现,其行为将与另一个实现相同.

If the standard report does not specify a return or mentions that it is undefined an implementation is literally free to choose the value returned and it would be according to the standard. That also means you cannot depend on one implementations behaviour would be the same as another.

以R6RS为例:

(if #f #t) ; ==> "banana"

...完全正确.由于谓词的计算结果为#f,并且没有提供的替代表达式,因此实现选择字符串"banana"作为其结果.有点不合常规,但仍然是标准中未定义的完美值 .

...is totally correct. Since the predicate evaluates to #f and there is no supplied alternative expression the implementation chose the string "banana" as its result. Slightly unconventional but still a perfectly nice value not defined in the standard.

选择可以使用的明智值可能会给用户带来不好的主意,使他们在以其他但符合标准的实现方式运行时,会误以为他们编写了错误的代码.因此,您有许多实现实际上将一个值定义为唯一未定义的值,并且它将代替报表中所有未定义的值,并且通常被REPL忽略.

Choosing sensible values that can be used might give the users bad ideas that might fool them into making erroneous code when run in a different, but standard compliant, implementation. Thus you have many implementation actually define a value to be the only undefined value and it would be used in place of all undefined values in the report and often ignored by the REPL.

以下是一些在不同实现中评估(list (if #f #t))的示例.将其包装在list中会使REPL显示一个列表,该列表的值可能会被抑制:

Here are some examples evaluating (list (if #f #t)) in different implementations. Wrapping it in a list makes the REPL show a list with a value that might otherwise have been suppressed:

;; racket in r5rs
==> (#<void>)

;; chicken
==> (#<unspecified>)

;; ikarus
==> (#<void>)

;; gambit
==> (#!void)

;; mit-scheme
==> (#!unspecific)

;; biwa
==> (#<undef>)

这篇关于Scheme中定义表达式返回的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆