Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #45357
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: The extended Eaker-Pelc CASE as general control structure |
| Date | 2016-02-08 11:11 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2016Feb8.121100@mips.complang.tuwien.ac.at> (permalink) |
| References | <2016Feb6.191548@mips.complang.tuwien.ac.at> <n98pmj$1lca$2@gioia.aioe.org> |
"HAA" <someone@microsoft.com> writes:
>Anton Ertl wrote:
>> ...
>> On the benefits: Consider a loop with three exit conditions, with a
>> different action necessary on each exit, and afterwards a common
>> action is necessary. With the extended CASE you can write this as
>>
>> case
>> condition1 ?of exit-action1 endof
>> condition2 ?of exit-action2 endof
>> condition3 ?of exit-action3 endof
>> ...
>> next-case
>> common-action
>>
>> If you want to do this with EXIT or a multi-WHILE loop, things get
>> cumbersome. Of course, Forthers often try to rationalize such
>> problems as encouraging factoring or somesuch, but if it really is
>> better factoring, do you really need to restrict your tools to achieve
>> this; and the workarounds for such problems are not always better
>> factoring.
>
>But such examples are the exception - not the rule. It's cheaper (and
>IMO cleaner) to factor out a large multi-test loop than to create tools
>you'll rarely use to put it in-line.
Actually multi-exit loops are not that rare; every search loop has
multiple exits: one for the search and the other for reaching the end
of the search space. If we use CASE for every multi-exit loop, it
will not be rare. And the benefit IMO is that it is more readable
than BEGIN ... WHILE ... WHILE ... REPEAT ... ELSE ... THEN, and also
more readable than unnatural factorings based on using EXIT.
Sure, a common Forth approach is to provide several special, cheaper
solutions instead of an expensive general one, and let the user select
among these special solutions depending on the problem, and try to
bend the problem towards these special solutions if necessary.
And once you have that mindset, you tend to see all general solutions
as expensive, and bending the problem as a virtue. But in the present
case the general solution is cheap, and using it has the potential
benefit of more readability from always using the same approach for
multi-condition loops.
>WHILE exists and is an everyday word. This fact alone makes it worth
>learning how to use it to your advantage.
>
>: gcd ( n1 n2 -- n )
> begin begin
> 2dup > while tuck - again then
> 2dup < while over - again then
> drop ;
Cute. AGAIN THEN is REPEAT, and with more conventional indentation we
get:
: gcd ( n1 n2 -- n )
begin
begin
2dup > while
tuck -
repeat
2dup < while
over -
repeat ;
>: collatz ( u -- )
> \ print the 3n+1 sequence starting at u until we reach 1
> begin begin
> dup .
> 1 over = if drop exit then
> dup 1 and while 3 * 1+ again then
> 2/
> again ;
: collatz ( u -- )
begin
begin
dup .
dup 1 = if
drop exit then
dup 1 and while
3 * 1+
repeat
2/
again ;
But here we already use EXIT (making common code afterwards
cumbersome), because we cannot use WHILE in that spot to jump behind
the AGAIN.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2015: http://www.rigwit.co.uk/EuroForth2015/
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-06 18:15 +0000
Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-07 03:57 -0800
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 12:59 +0000
Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-08 09:48 -0800
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 18:10 +0000
Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-09 14:02 -0800
Short-circuit evaluation (was: The extended Eaker-Pelc CASE ...) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-10 16:10 +0000
Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-07 14:32 +0000
Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-07 14:53 +0000
Re: The extended Eaker-Pelc CASE as general control structure Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-07 15:56 +0000
Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-07 18:11 +0000
Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-08 12:04 +1100
Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 10:29 +0000
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 11:00 +0000
Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 12:28 +0000
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 15:26 +0000
Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 16:16 +0000
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 16:37 +0000
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 16:21 +0000
Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-11 21:12 +1100
Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-11 10:46 +0000
Re: The extended Eaker-Pelc CASE as general control structure JennyB <jennybrien@googlemail.com> - 2016-02-07 12:33 -0800
Re: The extended Eaker-Pelc CASE as general control structure stephen@mpeforth.com (Stephen Pelc) - 2016-02-08 10:38 +0000
Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-08 12:07 +1100
Re: The extended Eaker-Pelc CASE as general control structure anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-08 11:11 +0000
Re: The extended Eaker-Pelc CASE as general control structure Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-08 14:49 +0000
Re: The extended Eaker-Pelc CASE as general control structure "HAA" <someone@microsoft.com> - 2016-02-09 20:31 +1100
Re: The extended Eaker-Pelc CASE as general control structure hughaguilar96@gmail.com - 2016-02-09 12:38 -0800
Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-11 14:23 +0000
Re: The extended Eaker-Pelc CASE as general control structure "WJ" <w_a_x_man@yahoo.com> - 2016-02-11 19:45 +0000
csiph-web