multiple values vs proc calls 
Author Message
 multiple values vs proc calls

Can anyone tell me whether there is a real difference between the
following 2 procs:

  (lambda ()
     (call-with-values
        (lambda () (values 1 2 3 4 5))
        (lambda (one two three four five)
           one)))

  (lambda ()
      ((lambda (one two three four five)
         one) 1 2 3 4 5)

I don't really understand why you would use the first case in
preference to the second.

Nic Ferrier



Fri, 25 Jun 2004 04:32:03 GMT  
 multiple values vs proc calls

Quote:
Nic Ferrier writes:
> Can anyone tell me whether there is a real difference between the
> following 2 procs:

>   (lambda ()
>      (call-with-values
>         (lambda () (values 1 2 3 4 5))
>         (lambda (one two three four five)
>            one)))

>   (lambda ()
>       ((lambda (one two three four five)
>          one) 1 2 3 4 5)

> I don't really understand why you would use the first case in
> preference to the second.

Would you use either of them in preference to (lambda () 1)? Why?

But assume you have a procedure that produces five values, to be used
with different consumers. Your design choices are as follows:

(1) Producer returns the values as a multiple values. Consumer to be
    supplied with call-with-values; from simple to more complex cases
    depending on how well the values and arguments match:

      (call-with-values produce consume)
      (call-with-values (lambda () (produce arg)) consume)
      (call-with-values
         (lambda ()
            (produce arg))
         (lambda (one two three four five)
            (consume three one four one five)))

    The producer is easy to document.

(2) Producer returns the values in a data structure. Let it be a list.

      (apply consume (produce))
      (apply consume (produce arg))
      (apply (lambda (one two three four five)
                (consume three one four one five))
             (produce arg))

    The producer is easy to document but it allocates a superfluous
    data structure.

(3) Producer stores the values in a data structure.

      (let ((values (list * * * * *)))
         (produce! values)
         (apply consume values))
      ...

    Producer is less easy to document, as it has an extra argument to
    explain. And it needs a bang in its name.

(4) Producer takes the consumer as an extra argument and calls it with
    the values.

      (produce consume)
      (produce arg consume)
      (produce arg (lambda (one two three four five)
                      (consume three one four one five)))

    Producer is even less easy to document as it has an extra argument
    to explain, and this time the documentation needs to talk of the
    consumer. However, this style of programming is well established
    and the extra argument has a name - it is a continuation to the
    producer - so the burden is not too much.

    This appears simplest to use. There are also powerful ways of
    passing several continuations and letting producer decide who
    gets to consume the values.

It is really number (4) that you should be comparing to (1), but (2)
and (3) are also candidates. But no fair having both producer and
consumer inline - let somebody else decide what to do with your
producer, which you begin to write as follows:

  ;;; (produce
  ;;; returns five values^H^H...list of five^H^H^H...stores
  ;;; eikupasses five values toeiku^H^H^H^A^K;;; returns

  (define (produce
--
Jussi



Fri, 25 Jun 2004 05:09:39 GMT  
 multiple values vs proc calls

Quote:

>   (lambda ()
>      (call-with-values
>         (lambda () (values 1 2 3 4 5))
>         (lambda (one two three four five)
>            one)))

>   (lambda ()
>       ((lambda (one two three four five)
>          one) 1 2 3 4 5)

> I don't really understand why you would use the first case in
> preference to the second.

You wouldn't.  You would use call-with-values if you had a function,
defined elsewhere, that returned multiple values.  As in:

(lambda (point)
  (call-with-values
    (get-coordinates point)
    (lambda (x y)
         ...)))



Fri, 25 Jun 2004 09:01:30 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Call-by-value vs. call-by-need/name

2. Call by reference vs. call by value

3. Call by reference vs. call by value

4. Passing values in call to TASM proc

5. let vs. multiple-value-bind

6. calling a proc from proc

7. Calling a proc within a proc in tcl/tk8.0

8. (values (multiple-value-form)) whats the point?

9. Multiple-value-list with no values.

10. I need to call some user init_proc before calling any Tcl or User Defined proc

11. Multiple INCLUDE Vs multiple USE

12. Return Values vs Status Queries vs Status Messages

 

 
Powered by phpBB® Forum Software