Scheme 中的真值 [英] True values in Scheme

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

问题描述

方案标准中是否定义了真值的含义?或者 Scheme 评估器的实现者是否可以选择将其定义为:(define (true? x) (eq? x #t))?

解决方案

方案标准是否定义了真值的含义?

是的,真"的意思在 Scheme 中有明确定义.

Scheme 标准有很多,但在所有标准中只有两个布尔对象;它们具有外部表示 #t#f.R7RS 还要求布尔对象具有替代的外部表示 #true#false.但是,在条件测试中,除了 #f(在 R7RS 的情况下为 #false)之外,所有值都评估为真.其他 lisp,尤其是 Common Lisp,可能有不同的真实性概念.

R5RS 6.3.1布尔值:

<块引用>

在所有标准 Scheme 值中,只有 #f 在条件表达式.除了 #f,所有标准 Scheme 值,包括 #t、pairs、空列表、符号、数字、字符串、向量和过程算作真.

R6RS 5.7 布尔值:

<块引用>

虽然有一个单独的布尔类型,但任何 Scheme 值都可以用作条件测试的布尔值.在一个条件测试,所有值在这样的测试中都算作真,除了#f.本报告使用true"一词来指代除 #f 之外的任何 Scheme 值,使用false"一词来指代 #f.

R7RS 6.3 布尔值:

<块引用>

true 和 false 的标准布尔对象写为 #t#f.或者,它们可以写成 #true#false,分别.然而,真正重要的是 Scheme 条件表达式(ifcondandor, when,unless, do) 视为真或假....

在所有 Scheme 值中,只有 #f 在条件中算作假表达式.所有其他 Scheme 值,包括 #t,都算作真.

实现可以对 True 使用不同的定义吗?

<块引用>

或者Scheme评估器的实现者可以选择将其定义为:(define (true?x) (eq?x #t))?

在任何 Scheme 标准中都没有定义 true? 谓词.在 OP 示例中定义的这种谓词在真/假值的最常见用例中会产生误导,即在条件表达式中:

>(定义(真?x)(eq?x #t))>(真的? '())#F>(如果 '()"真的吗?没想到这个""真的吗?期待这个")"真的吗?没想到这个"

上面的过程测试一个值是否是布尔值#t,而不是它是否为真.任何不将所有非#f 值(包括#t)视为真的实现都不是Scheme 的一致实现.使用 true? 的 OP 定义来确定true"含义的实现;在实施中将是不合格的.

可以定义 true? 如下:

>(定义(真?x)(不是(不是x)))>(真的? '())#t>(真的?#t)#t>(真的?#f)#F

这将回答一个值在条件表达式中是否会被测试为真或假的问题.

请注意,Scheme 中还有一个 boolean? 谓词用于测试值是否为布尔值:

>(真的?#t)#t>(布尔值?#t)#t>(真的?0)#t>(布尔值?0)#F>(真的?#f)#F>(布尔值?#f)#t

Is the meaning of a true value defined in the Scheme Standard? Or does the implementator of the Scheme evaluator have the choice to define it as: (define (true? x) (eq? x #t))?

解决方案

Do Scheme Standards Define Meaning for True Values?

Yes, the meaning of "true" is well-defined in Scheme.

There are a number of Scheme standards, but in all of them there are only two boolean objects; these have the external representations #t and #f. R7RS also requires that the boolean objects have the alternate external representations #true and #false. But, in a conditional test all values evaluate as true except #f (and #false in the case of R7RS). Other lisps, notably Common Lisp, may have different concepts of truthiness.

R5RS 6.3.1 Booleans:

Of all the standard Scheme values, only #f counts as false in conditional expressions. Except for #f, all standard Scheme values, including #t, pairs, the empty list, symbols, numbers, strings, vectors, and procedures, count as true.

R6RS 5.7 Boolean values:

Although there is a separate boolean type, any Scheme value can be used as a boolean value for the purpose of a conditional test. In a conditional test, all values count as true in such a test except for #f. This report uses the word "true" to refer to any Scheme value except #f, and the word "false" to refer to #f.

R7RS 6.3 Booleans:

The standard boolean objects for true and false are written as #t and #f. Alternatively, they can be written #true and #false, respectively. What really matters, though, are the objects that the Scheme conditional expressions (if, cond, and, or, when, unless, do) treat as true or false....

Of all the Scheme values, only #f counts as false in conditional expressions. All other Scheme values, including #t, count as true.

Can Implementations Use Different Definitions for True?

Or does the implementator of the Scheme evaluator have the choice to define it as: (define (true? x) (eq? x #t))?

There is no true? predicate defined in any Scheme standard. Such a predicate defined as in OP example would be misleading in the most common use cases for true/false values, i.e., in conditional expressions:

> (define (true? x) (eq? x #t))
> (true? '())
#f
> (if '()
      "true? does not expect this"
      "true? expects this")
"true? does not expect this"

The above procedure tests whether a value is the boolean #t, but not whether it is true. Any implementation that does not treat all non-#f values (including #t) as true is not a conforming implementation of Scheme. An implementation that uses the OP definition of true? to establish the meaning of "true" within the implementation would be nonconforming.

One could define true? as follows:

> (define (true? x) (not (not x)))
> (true? '())
#t
> (true? #t)
#t
> (true? #f)
#f

This would answer the question of whether a value will test as true or false in a conditional expression.

Note that there is also a boolean? predicate in Scheme that tests whether a value is a boolean value:

> (true? #t)
#t
> (boolean? #t)
#t
> (true? 0)
#t
> (boolean? 0)
#f
> (true? #f)
#f
> (boolean? #f)
#t

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

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