let vs. multiple-value-bind 
Author Message
 let vs. multiple-value-bind

Hi, I'm considering a new coding style and since one of the main
purpose of any style should be to help the reader of code, I thought
I'd ask you what you think of it.

I'm working on code for graphics and find I often write let forms
like:

(let ((x (node-x node))
      (y (node-y node)))
  ...)

To show that the x and y are considered a pair I thought the following
would be easier to read:

(multiple-value-bind (x y) (values (node-x node) (node-y node))
  ...)

I guess I would also make a function (node-xy-values node) to return
the two values.

Which do you like best, the let or the multiple-value-bind as they are
written above (i.e. without the use of node-xy-values).

Since I'm also interested in Lisp internals, is there any performance
penalty whatsoever in using multiple-value-bind instead of let?

Hallvard
--
Hallvard Traetteberg
Dept. of Knowledge Based Systems
SINTEF SI (Center for Industrial Research)
Box 124 Blindern, 0314 Oslo 3
NORWAY

Tlf: +47 2 45 29 83 or  +47 2 45 20 10
Fax: +47 2 45 20 40



Thu, 06 Jul 1995 20:21:05 GMT  
 let vs. multiple-value-bind
 |
 | I'm working on code for graphics and find I often write let forms
 | like:
 |
 | (let ((x (node-x node))
 |       (y (node-y node)))
 |   ...)
 |
 | To show that the x and y are considered a pair I thought the following
 | would be easier to read:
 |
 | (multiple-value-bind (x y) (values (node-x node) (node-y node))
 |   ...)
 |
 | Which do you like best, the let or the multiple-value-bind as they are
 | written above (i.e. without the use of node-xy-values).

I think I prefer the LET cliche. I guess I can't give you a *good*
explanation of why I do, though. LET just looks better in my eyes :-)
With LET I can immediately tell what you're up do, but with the
MULTIPLE-VALUE-BIND I'd have to spend a little extra mental energy to
figure out what it is you're trying to do. Also, it appears that LET
comes out better when consulting your own Style Guide: "Always use the
most specific construct". Assuming, of course, that LET is more
specific than MULTIPLE-VALUE-BIND is this connection.

 | Since I'm also interested in Lisp internals, is there any performance
 | penalty whatsoever in using multiple-value-bind instead of let?

That might very well be the case. Try a couple of MACROEXPAND-1's in
your favorite CL to find out.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

  Research Scientist  Voicenet: +47 9 183100 ext. 275
  CRS Division        Faxnet:   +47 9 187109
  OECD HRP            Papernet: PO Box 173, N-1751 Halden, Norway

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~



Fri, 07 Jul 1995 16:37:06 GMT  
 let vs. multiple-value-bind

As I remarked to Hal in email, the "new" CLOS macro WITH-SLOTS should do
what he wants.

--mark



Sat, 08 Jul 1995 02:17:07 GMT  
 let vs. multiple-value-bind

Quote:
> | Since I'm also interested in Lisp internals, is there any performance
> | penalty whatsoever in using multiple-value-bind instead of let?

> That might very well be the case. Try a couple of MACROEXPAND-1's in
> your favorite CL to find out.

Some compilers compile MULTIPLE-VALUE-BIND directly without considering its
macroexpansion. Use DISASSEMBLE to get a better impression of how a compiler
handles this construct.

Bruno Haible



Sat, 08 Jul 1995 05:15:27 GMT  
 let vs. multiple-value-bind
Hallvard Tretteberg asks:

Quote:
> (let ((x (node-x node))
>       (y (node-y node)))
>   ...)

> (multiple-value-bind (x y) (values (node-x node) (node-y node))
>   ...)

> Which do you like best, the let or the multiple-value-bind as they are
> written above (i.e. without the use of node-xy-values).

Clearly, I prefer the second one. It expresses more precisely what you meant,
especially when x and y are likely to be useless without each other.

Using the LETF macro I talked about some days ago and a small convention -
writing [x y] for (values x y)  - you can write your code like this:

(letf (([x y] [(node-x node) (node-y node)]))
  ...)

An implementation of []:
(set-macro-character #\[
  #'(lambda (stream char) (cons 'VALUES (read-delimited-list #\] stream t)))
)
(set-macro-character #\] (get-macro-character #\)))

An implementation of LETF and LETF* can be found in the clisp package.

Bruno Haible



Sat, 08 Jul 1995 05:31:38 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. multiple-value binding let and let*

2. (let (flet (multiple-value-bind (etc.))))

3. Final SRFI 8: RECEIVE: Binding to multiple values

4. SRFI 8: RECEIVE: Binding to multiple values

5. Defining multiple-values binding forms

6. MULTIPLE-VALUE-BIND question

7. binding multiple values (Ex: Re: some small proposed changes to standard)

8. lambda list in MULTIPLE-VALUE-BIND?

9. with-slots and multiple-value-bind

10. Common Lisp summary and multiple value binding

11. multiple-value-bind

12. LET vs. LET*

 

 
Powered by phpBB® Forum Software