Sujet : Re: Loops (was Re: do { quit; } else { })
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 22. Apr 2025, 19:54:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vu8ok8$15ph8$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
User-Agent : Mozilla Thunderbird
On 22/04/2025 19:16, David Brown wrote:
On 22/04/2025 09:54, Janis Papanagnou wrote:
On 21.04.2025 17:36, bart wrote:
On 21/04/2025 14:51, Waldek Hebisch wrote:
>
[ sqlite3.c code ]
>
You have to analyse it first. The kind of loop this expresses is:
>
No. That's just *your* _transcription_ of the original code
>
p = startvalue()
>
while (p) {
<body>
p = nextvalue()
}
>
Notice how I chose to express it: it reflects how I would describe it in
English:
>
You are *not* describing _the task_, you are merely translating the
_chosen commands_ of the procedural language [that you have chosen]
>
>
* Set P to Start Value
* While P isn't Null:
* Execute the body
* Set P to Next Value
>
Whatever programming language and constructs you use, this is not
more than a rephrasing the used language constructs (as opposed to
the task).
>
(It's not surprising; you can observe that folks that are familiar
only with imperative languages tend to this sort of fallacy.)
>
So, how would /you/ describe it in English? (Or in any language if like,
as the ordering is more important.)
>
Iterate over the elements and sequentially perform <the task> on
the elements. Or a bit more formally
>
iterate (elements, task)
>
in some functional programming language. Or any other variant on
any abstraction level that is appropriate.
>
Alternatively, you might describe it as "Perform the task on each element" :
task(e) for e in elements
Sometimes you want to emphasise moving through the elements, sometimes you want to emphasise what you are doing with them - but either way is fine.
You may or may not include an order in the iteration, depending on what you actually want to do here (I have no idea of the purpose of the original code).
I would suggest Bart thinks this through using a real-world task. Consider making a cake by mixing the ingredients you have laid out on your kitchen top. Would you use Janis' formulation?
Go through the ingredients, putting each in the bowl.
Or mine?
Put in the bowl each of the ingredients.
Or Bart's ?
Stand in front of the first ingredient.
While you have an ingredient in front of you :
Put the ingredient in the bowl.
Move to the next ingredient.
Well C's would be:
For (first ingredient; ingredient; next ingredient)
We /are/ still about expressing a loop in C, yes? At least mine directly translates to C:
x = first_ingredient(); // C needs some state
while (x != empty) {
put_ingredient();
x = next_ingredient();
}
I'd be interested in how your 'Put in the bowl each of the ingredients' translates into:
FOR(W; X; Y) {Z;}
There is also nothing about this syntax that directly indicates the middle term X is the terminating condition; it is just expression #2 of 3.
It doesn't read as English; not in this form. Whereas FOR I IN A TO B is already most of the way there, as is WHILE (CONDITION).