2

From the R5RS standard:

Values might be defined as follows:
(define (values . things)
  (call-with-current-continuation
    (lambda (cont) (apply cont things))))

My first interpretation of this was that an expression like (+ (values 1 2)) is equivalent to (apply + '(1 2)) and would yield the result 3. But, according to my tests, this interpretation is not correct. Here is my interpretation of the code above: values is a function taking any number of arguments, bundled into a list called things. Then, the current continuation (the place where values is used) is called with the list things "unbundled".

What am I missing? The example above (+ (values 1 2)) gives an error or 1 depending on the interpreter I used.

4

2 回答 2

5

看,当你输入

(+ (values 1 2))

继续调用 tovalues实际上+. 因此,它要么被视为 1(列表的第一个元素,过程产生的第一个值),要么被视为错误。R5RS 对此表示:

除了由 call-with-values 过程创建的延续之外,所有延续都只取一个值。未指定向不是由 call-with-values 创建的延续传递任何值或多个值的效果。

另一方面,call-with-values将正确地将列表的元素绑定到其consumer参数的形式参数:

调用没有值的生产者参数和一个延续,当传递一些值时,调用使用这些值作为参数的消费者过程。

于 2018-04-03T23:15:43.937 回答
2

为了理解这个定义的values含义,您还需要理解 的定义call-with-current-continuation,它是根据它来定义的。values有用的是,提及的文档call-with-values,作为如何使用结果的示例values

因此,您可以(values 1 2)在以下上下文中使用:

(call-with-values (lambda () (values 1 2))
                  (lambda (x y) (+ x y)))
于 2018-04-03T23:16:46.767 回答