开发者

When to use (values ...) (define-values ...) in Scheme

开发者 https://www.devze.com 2022-12-10 04:46 出处:网络
I have read documentation for functions such as values and define-val开发者_开发问答ues that return and consume multiple values. I understand what they do. It\'s not clear to me when you would want to

I have read documentation for functions such as values and define-val开发者_开发问答ues that return and consume multiple values. I understand what they do. It's not clear to me when you would want to use such a thing.

When would it be bad/impossible to build a single list of values and consume that single list of values instead?


define-values is a convenience that lets you directly bind variables to the results of a expression. It saves you some typing as you don't have to explicitly unpack a list. I don't think there are situations where it is bad or impossible to build a single list of values. In fact, that will be more portable than define-values.


Here is my original post on the topic; it is copied below.

In this thread in comp.lang.scheme the means to return multiple values are discussed. There are seemingly 3 solutions in R6RS:

(import (rnrs))

; let-values + values
(define (foo1)
  (values 1 2 3))

(let-values (((a b c) (foo1)))
  (display (list a b c))
  (newline))

; cps
(define (foo2 k)
  (k 1 2 3))

(foo2 (lambda (a b c) 
        (display (list a b c))
        (newline)))

; list
(define (foo3)
  (list 1 2 3))
(let ((result (foo3)))
  (display result)
  (newline))

Per Aziz and Aaron’s point; you should use the approach that communicates the most information to the reader.

0

精彩评论

暂无评论...
验证码 换一张
取 消