Google Groups Home
Help | Sign in
Message from discussion Shorter/better way to do this?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
jzakiya  
View profile
 More options 15 Oct 2008, 05:49
Newsgroups: comp.lang.forth
From: jzakiya <jzak...@mail.com>
Date: Tue, 14 Oct 2008 21:49:38 -0700 (PDT)
Local: Wed 15 Oct 2008 05:49
Subject: Re: Shorter/better way to do this?
On Oct 14, 6:50 pm, William James <w_a_x_...@yahoo.com> wrote:

> On Oct 14, 12:44 pm, jzakiya <jzak...@mail.com> wrote:

> > I have a string of integer values:

> > p1<p2<p3<p4...<p8 and a reference value lndx

> > I have a code snippet that I have to do this way

> > begin p8 lndx < while
> >    0 p1 sieve c!  0 p2 sieve c!  0 p3 sieve c!  0 p4 sieve c!
> >    0 p5 sieve c!  0 p6 sieve c!  0 p7 sieve c!  0 p8 sieve c!
> >    x1 p1 + to p1   x1 p2 + to p2    x1 p3 + to p3    x1 p4 + to p4
> >    x1 p5 + to p5   x1 p6 + to p6   x1 p7 + to p7    x1 p8 + to p8
> > repeat
> > p7 lndx < if 0 p7 sieve c! then
> > p6 lndx < if 0 p6 sieve c! then
> > p5 lndx < if 0 p5 sieve c! then
> > p4 lndx < if 0 p4 sieve c! then
> > p3 lndx < if 0 p3 sieve c! then
> > p2 lndx < if 0 p2 sieve c! then
> > p1 lndx < if 0 p1 sieve c! then

> > So when p8 >= lndx I want to check the other pi < lndx once
> > more and if true, then do  0 pi sieve c! FOR ALL OTHER pi.

> > Conceptually, I want to do something like this:

> > p7 < lndx IF goto 7 elsif p6 < lndx if goto 6 elsif ...p1 < lndx goto
> > 1

> > 7: 0 p7 sieve c!
> > 6: 0 p6 sieve c!
> > 5: 0 p5 sieve c!
> > 4: 0 p4 sieve c!
> > 3: 0 p3 sieve c!
> > 2: 0 p2 sieve c!
> > 1: 0 p1 sieve c!

> > So, if pi < lndx I want to "goto" the ith label and do all the
> > operations
> > starting there to the end thru p1, since we know that if pi < lndx
> > then
> > all the pi-1,2,..etc < lndx will also be true.

> > The way I've done it was the shortest/fastest way I was able to do
> > this
> > correctly, writing, I believe, the smallest amount of repetitive code.

> > Is there a shorter/faster way to do this?

> Instead of p1 ... p8, use array p.

> Something like this (untested).

> Ruby:

> p = Array.new(8){ 0 }

> top = nil
> 7.downto(0){|i|
>   if p[i] < lndx
>     top = i
>     break
>   end}

> top.downto(0){|i|
>   sieve[ p[i] ] = 0

> }

Maybe you haven't noticed but this is a Forth group and NOT Ruby.

FYI I use Ruby, and the Ruby versions of this code does not use nor
need these constructs because Ruby has dynamics arrays, which I can
grow or shrink to my hearts content. ONLY because arrays in Forth are
static do I have to resort to the described mechanism to fit stuff
into the array size domain of the "sieve" array, otherwise I could let
the array grow (as I do in Ruby) and then shrink it back to the
desired length when it matters.

Also FYI, you are not making Ruby friends by being a PITA about
posting none germain Ruby code to a specific Forth coding request. I
was hoping maybe there is some nice CASE structure, et al, that can do
what I want, in a more elegant and/or faster way.

When in Forthland speak Forth, please.

Jabari


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google