Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Counted loops (...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 18 Topic 4048 of 4053
Post > Topic >>

Counted loops (was: [SPOILER] Re: Euler problem #187)

by anton@[EMAIL PROTECTED] (Anton Ertl) May 10, 2008 at 05:34 PM

mhx@[EMAIL PROTECTED]
 (Marcel Hendrix) writes:
>anton@[EMAIL PROTECTED]
 (Anton Ertl) writes Re: [SPOILER] Re:
Euler problem #187
>>> I can not read the code with these funny loops.
>
>> Read up on them in
>
>>
http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Counted-Loops.html
>
>> They are just improved versions of the regular counted loops.  I guess
>> I would have made another error or two with the regular counted loops.
>
>They may be technically superior, but having to look up the 8 (?) 
>variants every time undoes any possible benefit for me.

I think it's pretty mnemonic (see below), and it saves you remembering
and working around the traps that the Forth-94 counted loops lay out
for you.

Let's first look at the DOs: You can have DOs for up-counting loops
(+DO, U+DO), and DOs for down-counting loops (-DO, U-DO).  The other
dimension is whether the limits are signed (+DO, -DO) or unsigned
(U+DO, U-DO).  So this gives a table

            signed unsigned
count up      +DO    U+DO
count down    -DO    U-DO

Next, let's look at the LOOPs: there is the usual LOOP and +LOOP for
counting up, and the new -LOOP for counting down.

I don't use counted loops often, but I don't need to look these words
up when I need them.

>I really do 
>not understand what 'problem' the Gforth manual sees in 
>
>: test  0 3 do i . -1 +loop ; test  3 2 1 0  ok

 : test  3 0 do i .  1 +loop ; test 0 1 2  ok

If you don't see a problem, you are blind.

Here's a better solution for counting down:

 : test  0 3 do i .  1 -loop ; test 3 2 1  ok

(Note that the compat/loops.fs version of -LOOP only works with -DO and
U-DO, not with DO or ?DO).

The other problems with your test loop construction is that it uses
DO, and the problem can be seen with:

: test1 do i . -1 +loop ;
0 0 test1
0 -3 test1

These can be avoided by using -DO.  

There is only one excuse for using DO, and knowing that the parameters
are distinct is not one of them.

>FORTH> euler187
>There are 17427272 composite integers, n < 10^8, having precisely two,
not
>necessarily distinct, prime factors.
>0.250 seconds elapsed. ok

It seems you have worked at getting the wrong answer way faster?

- 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 2008:
http://www.complang.tuwien.ac.at/anton/euroforth/ef08.html




 18 Posts in Topic:
[SPOILER] Re: Euler problem #187
anton@[EMAIL PROTECTED]   2008-05-09 20:27:46 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 00:42:26 
Re: [SPOILER] Re: Euler problem #187
cac <cac@[EMAIL PROTEC  2008-05-09 16:23:19 
Re: [SPOILER] Re: Euler problem #187
anton@[EMAIL PROTECTED]   2008-05-10 09:43:00 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 12:45:19 
Re: [SPOILER] Re: Euler problem #187
anton@[EMAIL PROTECTED]   2008-05-10 13:11:43 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 19:29:58 
Counted loops (was: [SPOILER] Re: Euler problem #187)
anton@[EMAIL PROTECTED]   2008-05-10 17:34:43 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 20:10:28 
Re: [SPOILER] Re: Euler problem #187
Luca Masini <lmasini@[  2008-05-10 23:54:21 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-11 00:58:55 
more euler problems, was Re: Euler problem #187
Albert van der Horst <  2008-05-12 14:21:47 
[SPOILER] Re: more euler problems
anton@[EMAIL PROTECTED]   2008-05-12 18:43:32 
Re: [SPOILER] Re: more euler problems
cac <cac@[EMAIL PROTEC  2008-05-12 13:26:55 
Re: [SPOILER] Re: more euler problems
anton@[EMAIL PROTECTED]   2008-05-13 10:14:55 
Re: [SPOILER] Re: more euler problems
cac <cac@[EMAIL PROTEC  2008-05-13 10:16:55 
Re: [SPOILER] Re: more euler problems
anton@[EMAIL PROTECTED]   2008-05-13 18:22:29 
Re: [SPOILER] Re: more euler problems
cac <cac@[EMAIL PROTEC  2008-05-13 12:25:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 1:27:36 CDT 2008.