A question of style
Ge' Weijers
ge at kunivv1.sci.kun.nl
Fri Dec 8 20:54:49 AEST 1989
amull at Morgan.COM (Andrew P. Mullhaupt) writes:
>I would like to elaborate: There are no good excuses for the comma
>operator because it masquerades as true concurrent assignment where
>it is a poor relation of it. The thing invites you to cram all sort
>of gooey side effects into for loops with increment and assignment
>operators. (This is the "Damn the Correctness! Full Speed Ahead"
>school of programming.) Now with real concurrent assignment, you
>really can put your loop invariant maintenance in one place:
>(i++ , j--)
>can be replaced by the much more attractive:
>(i, j) := (i + 1, j - 1)
The comma operator has nothing to do at all with the concurrent assignment.
The left operand is evaluated only for its side-effect. It is most useful
when implementing macros that need to do multiple things. (look at the
expansion of getchar() to see my point). The concurrent assignment is
very useful, as it lets the compiler worry about difficult constructs,
in stead of you. Example: the pointer rotation.
(p is a pointer to a binary tree node with left and right fields)
(p, p->right, p->right->left) := (p->right, p->right->left, p);
Try this in C. The above is more elegant.
The obvious way:
tmp1 = p->right; tmp2 = p->right->left; tmp3 = p;
p = tmp1; p->right = tmp2; p->right->left = tmp3;
Now what did I do wrong :-)
It makes writing down things like that much easier, the compiler worries
about keeping things in temporaries, figuring out optimal ordering etc.
Useful, ain't it?
(forgive my ramblings, but I though Andrew Mulhaupt's example was a little
oversimplified. concurrent assignment more useful than that. C is definitely
not the last word in programming languages, whether ANSI, ++ or whatever.)
Ge' Weijers
Ge' Weijers Internet/UUCP: ge at cs.kun.nl
Faculty of Mathematics and Computer Science, (uunet.uu.net!cs.kun.nl!ge)
University of Nijmegen, Toernooiveld 1
6525 ED Nijmegen, the Netherlands tel. +3180612483 (UTC-2)
More information about the Comp.lang.c
mailing list