Re: The LOOP macro (was Re: Be afraid of XML)
RobertMaas_at_YahooGroups.Com
Date: 03/14/04
- Next message: Pascal Bourguignon: "Re: Be afraid of XML"
- Previous message: pkhuong: "Re: LISP.NET? I love Lisp, it makes me an elitest."
- In reply to: Barry Margolin: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Next in thread: Jens Axel Søgaard: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Reply: Jens Axel Søgaard: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Reply: Pascal Bourguignon: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 14 Mar 2004 14:11:20 -0800
> From: Barry Margolin <barmar@alum.mit.edu>
> Date: Sat, 13 Mar 2004 14:23:03 -0500
> The thing that has always bugged me about C's for loop is that the
> order of the 2nd and 3rd clauses (the end-test and the variable
> iteration) seemed backward, and there's nothing in the syntax that
> helps you to remember which is which
And one of the standard textbooks doesn't help either. See Forouzan &
Gilberg "A structured programming course using C", second edition
(2001), ISBN 0-534-37482-4, flowcharts on page 240 (with some arrows
added by me to make the flow clearer, and + at each bend in line):
Left diagram:
|
V
-------------
/ | expr1 \ false
+--->< expr3 |-------->---->+
^ \ | expr2 / |
| ------------- |
| | true |
| V |
| +-----------+ |
| | statement | |
| +-----------+ |
| | |
| V |
+<-------------+ |
V
+<-----------+
|
V
Bad things about that diagram:
(1) The three expressions aren't physically placed in the same sequence
as in source code, so it's of no use to aid memorizing which is which;
(2) The false clause seems to be coming out from the mess *before*
expr2 is evaluated, when in fact expr2 is the value tested zero/nonzero
(fake boolean) to see whether it's false or true respectively;
(3) The arrows entering the top mess at points expr1 and expr3 do show
which get executed first (initially, and after each loop,
respectively), and the arrow slightly displaced to the right from the
boundary between expr3 and expr2 does show that expr2 is last before
the statement executes, but that still doesn't show clearly that
control then passes from expr1 or expr3 to expr2 *only*, never from
expr1 to expr3 or vice versa;
Right diagram:
|
V
+-------+
| expr1 |
+-------+
|
V
/-----\ false
+--->< expr2 >-->+
^ \-----/ |
| | true |
| V |
| +-----------+ |
| | statement | |
| +-----------+ |
| | |
| V |
| +-------+ |
| | expr3 | |
| +-------+ |
| | |
| V |
+<-------+ |
V
+<-------+
|
V
At least the flowchart makes the flow clear, and the three expressions
are shown in the correct linear sequence, but:
Bad things about that diagram:
(1) The three expressions are shown vertically, rather thna
horizontally as is usual in the syntax;
(2) There's a big gap between expr2 and expr3, which might mislead the
programmer into remembering the syntax as:
for (expr1; expr2) statement (expr3)
Or if the syntax is remembered, it's hard to remember where in the
diagram to insert the statement.
When I was taking that class from that book, I had to invent my own
version of that diagram to remember the relation between flowchart and
C syntax:
|
V
+-------+ /---------\ +-------+
| expr1 |-->< expr2 ><--| expr3 |
+-------+ \---------/ +-------+
false | | true ^
V | |
+<------------+ | |
| V |
| +-----------+ |
| | statement | |
| +-----------+ |
| | |
| V |
| +---------->+
|
V
That's a bit nonstandard, having both branches of the conditional test
coming out the bottom instead of one out the bottom and one out the
side, but otherwise does that help everyone remember how the syntax
fits the flow, in case you ever are required to (yuk) write in C again?
> contrast it with PL/I and Algol's "from ... by ... to ..." syntax,
> which was adopted into LOOP
And LOOP in LISP has the added advantage that the many key words within
the LOOP-macro syntax are *not* reserved words within the entire
language as they presumably are in those other languages, in fact even
within a LOOP expression they aren't even reserved words in the sense
of affecting the parser. They only affect the semantics after the
parser is all done and EVAL or COMPILE is in control.
- Next message: Pascal Bourguignon: "Re: Be afraid of XML"
- Previous message: pkhuong: "Re: LISP.NET? I love Lisp, it makes me an elitest."
- In reply to: Barry Margolin: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Next in thread: Jens Axel Søgaard: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Reply: Jens Axel Søgaard: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Reply: Pascal Bourguignon: "Re: The LOOP macro (was Re: Be afraid of XML)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|