[racket] Splicing `values' in-place

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Thu Jul 11 11:27:59 EDT 2013

FWIW, several years ago, I implemented a wrapper for "#%app" that 
supported this, as an exercise and without actually using it, but I kept 
it in mind.

I use multiple-values a lot, including in named-"let" recursion, and, 
since that multiple-value-splicing "#%app" exercise, I have noticed 
times when it would have been a win.  But I have also noticed at least 
as many times when I had a coding error that multiple-value-splicing 
would've obscured.  I intuitively think it would be bad for readability, 
but I don't have even anecdotal data for that.

Of course, people with other coding styles or different kinds of 
programs would put different weights on the pros and cons.  Do you think 
people would be happy with an "app" macro that they'd use only when then 
wanted to so an apply with multiple-values splicing?  It's easy to 
implement in an inefficient way (below is such an inefficient 
implementation I made for a blog post a long time ago):

(define-syntax app
  (syntax-rules ()
    ((_ PROC)         (PROC))
    ((_ PROC ARG ...) (%app:1 (ARG ...) () (PROC)))))

(define-syntax %app:1
  (syntax-rules ()
    ((_ () (BIND ...) (PROC VAR ...))
     (let (BIND ...)
       (apply PROC (append VAR ...))))
    ((_ (ARG0 ARG1 ...) (BIND ...) (PROC VAR ...))
     (%app:1 (ARG1 ...)
             (BIND ... (actual (call-with-values (lambda () ARG0) list)))
             (PROC VAR ... actual)))))

(app vector 1 (values 2 3) 4 (list 5) (values 6 7))
;;==> #(1 2 3 4 (5) 6 7)

I haven't really needed such a macro myself so far.  Before I'd want 
that macro, I would want things like multiple-value support in "let".

Neil V.

Laurent wrote at 07/11/2013 08:56 AM:
> In some postfix languages, if a procedure returns multiple values, 
> these values can be used directly as multiple arguments to another 
> procedure call, i.e., they are "spliced" in the latter call.
> In an extended Racket, this would look like this:
>
> (+ (values 1 2) (values 3 4))
> would be equivalent to
> (+ 1 2 3 4)
>
> (map values '(0 1 2) '(a b c))
> would return
> '(0 a 1 b 2 c)
>
> (call-with-values (lambda()(my-proc ....)) list)
> would simply be
> (list (my-proc ....))
>
> (values (values 1 2) (values 'a 'b))
> would be equivalent to
> (values 1 2 'a 'b)
>
> Correct me if I'm wrong, but I think all the cases where this feature 
> should be useful currently throws an error, so it would probably break 
> only very little.
>
> Such a missing feature tickles me from time to time, and I often find 
> that Racket `values' system is too cumbersome to be used more often, 
> i.e., you need to go through stages of `call-with-values', 
> 'let/define-values', `(apply values ....)', etc. and I often find 
> myself not wanting to go down this road.
>
> IMO, `values' is *meant* to be the way I describe above: `values' is 
> exactly like `list', except than instead of encapsulating the values 
> in a container, it splices them in-place.
>
> Do you see some disadvantages of using values this way?
> For example, in some occasions, for things like
> (define (foo x) (values x x))
> (map + (foo '(1 2 3)))
> it may be more difficult to infer that there are actually 2 lists in 
> the map, but to me it's just a matter of 
> style/taste/comments/documentation, not a matter of feature.
>
> Laurent
>
>
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users
>    
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130711/e603bd9d/attachment-0001.html>

Posted on the users mailing list.