Common Lisp has a "special operator" called multiple-value-call, which does something similar to Scheme's call-with-values but with a nicer syntax, and allowing multiple producers (unlike call-with-values). So I thought I'd try to implement multiple-value-call in Scheme, using call-with-values (dressed up here as receive) behind the scenes:

(define-syntax multiple-value-collect
  (syntax-rules ()
    ((multiple-value-collect) '())
    ((multiple-value-collect expr next ...)
     (receive vals expr
       (cons vals (multiple-value-collect next ...))))))

(define-syntax multiple-value-call
  (syntax-rules ()
    ((multiple-value-call func expr ...)
     (apply func (concatenate! (multiple-value-collect expr ...))))))

Is there a better (more idiomatic, performant, etc.) way to implement this? (concatenate! is from SRFI 1, and receive is from SRFI 8.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.