The need for D-scussion (was Re: D Wishlist)

Karl Heuer karl at haddock.ISC.COM
Sat Mar 26 11:42:29 AEST 1988


In article <10763 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>In article <719 at l.cc.purdue.edu> cik at l.cc.purdue.edu (Herman Rubin) writes:
>>Furthermore, returning a list of values for a function is far different from
>>returning a structure.
>
>If so, the compiler should be improved.  There is no theoretical difference.

Or perhaps the language should be improved.  In C, the div() function returns
a struct.  If I want to add the squares of the two components, I have to write
either
  qr = div(x, y);
  z = qr.quot * qr.quot + qr.rem * qr.rem;
or use a function
  int f(struct div_t qr) { return (qr.quot * qr.quot + qr.rem * qr.rem); }
  z = f(div(x, y));
The former requires a throwaway variable; the latter, a throwaway function.

In Lisp, you're still returning an aggregate.  Things are a little better,
because you can tighten the scope:
  (setq z
        ((lambda (qr) (+ (* (car qr) (car qr)) (* (cdr qr) (cdr qr))))
         (div x y)))

In Forth, you can actually return multiple values (as opposed to returning a
single value of aggregate type):
  x @ y @ div  dup *  swap dup *  +  z !

There's a certain elegance here which is lacking in the first two.  The qr
temporary has been absorbed by the syntax of the language, in exactly the same
manner as the temporary results of the "*" operator in all three examples.

I don't know if this is what Herman was talking about, but in this sense the
current C model is lacking.  Whether this could be "fixed" in a C-like
language is another question.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list