我正在使用球拍和博士球拍用于教育目的。
我正在尝试理解值内置函数。
当我使用以下输入调用值时,将得到以下输出:
> (values 'this 'and-this 'and-that)
'this
'and-this
'and-that为什么这个测试失败了?
#lang racket
(require rackunit racket/trace)
(check-equal? (values 'this 'and-this 'and-that) 'this 'and-this 'and-that)发布于 2016-11-18 00:06:45
因为values正在返回多重值,而check-equal?无法处理这个问题,所以它需要比较两个值。例如,这是因为我们只处理两个值:
(check-equal? (values 42) 42)尝试一种不同的方法,假设我们首先解压缩多个值并将它们放在列表中:
(check-equal?
(let-values ([(a b c) (values 'this 'and-this 'and-that)])
(list a b c))
'(this and-this and-that))发布于 2016-11-18 09:16:31
可以将多个值转换为列表,然后将其与预期值进行比较:
(define-syntax values->list
(syntax-rules ()
((_ expr)
(call-with-values (lambda () expr)
list))))
(check-equal? (values->list (values 'this 'and-this 'and-that))
(list 'this 'and-this 'and-that))values是非常特别的。它和call-with-values有联系。实际上,它的工作原理与list和apply基本相同,但没有在两者之间创建逆操作。因此,只是一个优化。在使用堆栈的实现中,它将返回堆栈上的值,而call-with-values将应用于应用thunk时获得的相同帧。
我确实认为Common版本更有用,因为它允许在单值情况下使用多个值过程,只使用第一个值。在Scheme方言中,您需要使用call-with-values或使用它的宏或过程来处理多个值,这是不太灵活的,但可能可以执行其他更有效的调用。
https://stackoverflow.com/questions/40665723
复制相似问题