Talk About Network

Google


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 > Re: Elegant cod...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 35 of 58 Topic 4000 of 4173
Post > Topic >>

Re: Elegant code Was: Re: what kind of non microconroller app are

by Gerry <gerry@[EMAIL PROTECTED] > Apr 21, 2008 at 02:15 AM

On 21 Apr, 06:58, Jonah Thomas <jethom...@[EMAIL PROTECTED]
> wrote:
> Gerry <ge...@[EMAIL PROTECTED]
> wrote:
> > I've emailed you a copy. I'd appreciate it if you let me know how you
> > got on with it or if the email doesn't reach you
>
> It arrived intact. I've spent half an hour looking at it, I can follow
> in general what it's doing. It looks more complicated than I expected. I
> probably haven't noticed all the subtle problems that need to be solved.
>
> I want to ask anyone who's interested about a programming puzzle I ran
> into when I tried something similar.
>
> The idea is to have a routine GOOD-STRING that accepts a string ( ca len
> ) and an execution token that acts on the string and checks the first
> part of the string for some condition. Like, it couild test whether a
> character is an uppercase letter, or whether it's a digit, or
> alphanumeric, or whether two characters are ":=3D" or whatever. The
> execution token should accept an address in the string, and it returns a
> flag.
>
> XT ( ca -- flag )
>
> The routine does ( ca len xt -- ca len' ) . It returns as much of the
> string as meets the condition the execution token tests for.
>
> So I'd wind up with:
>
> : STRING-CHECKER ( S: xt "name" -- )
> ( child: ca len -- ca len' )
> =A0 =A0CREATE ,
> =A0 =A0DOES> @[EMAIL PROTECTED]
 GOOD-STRING ;
>
> to make a collection of named routines to check strings in various ways.
>
> My first attempt at GOOD-STRING was like this:
>
> : GOOD-STRING1 ( S: ca len xt -- ca len' )
> =A0 =A0>R
> =A0 =A02DUP BEGIN
> =A0 =A0 =A0DUP 0 > WHILE =A0
> =A0 =A0 =A0 =A0OVER R@[EMAIL PROTECTED]
 EXECUTE WHILE
> =A0 =A0 =A0 =A0 =A01 /STRING
> =A0 =A0 =A0REPEAT
> =A0 =A0THEN
> =A0 =A0NIP - RDROP ;
>
> This works adequately but it's clumsy. A complicated loop and a return
> stack item. No way to simplify it that matters, not without getting rid
> of either the complex loop or the >R or both. Using locals would handle
> it easily but I didn't want to do that. I thought there ought to be a
> good way to do this without locals. I only care about 3 items, surely I
> can handle 3 stack items.
>
> So I tried to get rid of the loop with recursion. I had just the looping
> part recurse.
>
> : CHECK-STRING2 ( ca len xt -- ca' len' xt )
> =A0 =A0OVER 0 > IF
> =A0 =A0 =A0>R OVER R@[EMAIL PROTECTED]
 EXECUTE IF
> =A0 =A0 =A0 =A01 /STRING R> RECURSE
> =A0 =A0 =A0ELSE
> =A0 =A0 =A0 =A0R>
> =A0 =A0 =A0THEN
> =A0 =A0THEN ;
>
> This is not great. My first recursive approach was even worse. I tried
> to get rid of the xt, but then I needed extra branches to get rid of it
> consistently. Easier to DROP it once when the routine is done. I figure
> branches are a little better than loops and there's only one RECURSE
> isntead of two WHILEs. But it isn't simple and there's no obvious way to
> factor it.
>
> I could get rid of the return stack ops by using DEFER .
>
> DEFER CHECKER
> : CHECK-STRING3 ( ca len -- ca' len' )
> =A0 =A0DUP 0 > IF
> =A0 =A0 =A0OVER CHECKER IF
> =A0 =A0 =A0 =A01 /STRING RECURSE
> =A0 =A0 =A0THEN
> =A0 =A0THEN ;
>
> The DOES> child can simply put the xt into CHECKER and it works. Simple.
> But not easily re-entrant. I will want to be checking a string and in
> the middle I'll need to start checking for something else, and then
> continue where I left off. To do that I have to save the old value of
> CHECKER and there's no standard way to get that. I'd have to save the
> old value on a stack. The complication pops up there.
>
> So I did without the deferred variable.
>
> : CHECK-STRING4 ( xt ca len -- xt ca' len' )
> =A0 =A0DUP 0 > IF
> =A0 =A0 =A0OVER 2OVER DROP EXECUTE IF
> =A0 =A0 =A0 =A01 /STRING RECURSE
> =A0 =A0 =A0THEN
> =A0 =A0THEN ;
>
> Changing the stack order helped some. The return stack mess is gone, and
> the side branches. The only troublesome bit is a little spot of stack
> noise. Would 3PICK look better than 2OVER DROP ?
>
> I can make it look nicer.
>
> : CHECK-STRING5 ( xt ca len -- xt ca' len' )
> =A0 =A03DUP 0 > IF
> =A0 =A0 =A0SWAP EXECUTE IF 1 /STRING RECURSE THEN
> =A0 =A0THEN ;
>
> 3DUP is not really simpler than 3PICK . It's easier for me to see what's
> going on, but the obvious way to implement 3DUP in high-level standard
> Forth is : 3DUP 2PICK 2PICK 2PICK ;
>
> Only 3 stack items. Why is it so hard? Because with ( a b c ) the things
> we need to do require copies
>
> len
> ca xt
> ca len =A0 =A0 =A0original, not copy
> xt ca len =A0 original
>
> There's no way to get all four patterns without some stack juggling. (Or
> locals.)
>
> When is this sort of perfectionism justified? I got something that
> worked in 3 minutes. After a series of false starts I got something that
> approched elegance. It still has a RECURSE so it wouldn't be as easy to
> debug at the keyboard as something that didn't have recursion or loops.
> Luckily it passed my tests the first try.
>
> : GOOD-STRING2 { ca len xt }
> =A0 =A0ca len BEGIN
> =A0 =A0 =A0DUP 0 > WHILE =A0
> =A0 =A0 =A0 =A0OVER xt EXECUTE WHILE
> =A0 =A0 =A0 =A0 =A01 /STRING
> =A0 =A0 =A0REPEAT
> =A0 =A0THEN
> =A0 =A0NIP ca len ROT - ;
>
> A few locals are enough to get past some of the worst problems. And if a
> few locals are good, maybe more locals are better. With 2 more locals
> and liberal use of TO I can eliminate every stack word.
>
> : GOOD-STRING2 0 0 { ca len xt ca' len' }
> =A0 =A0ca TO ca' len TO len' BEGIN
> =A0 =A0 =A0len' 0 > WHILE =A0
> =A0 =A0 =A0 =A0ca' xt EXECUTE WHILE
> =A0 =A0 =A0 =A0 =A0ca' len' 1 /STRING TO len' TO ca'
> =A0 =A0 =A0REPEAT
> =A0 =A0THEN
> =A0 =A0ca len len' - ;
>
> Better? Not only do you eliminate the stack words, the stack comments
> are part of the code.
>
> When is it worthwhile to make the code simple? I have to figure the
> ideal approach is to do it simply the first time. If you write elegant
> code quickly, so the first version is easy to understand and easy to
> test and easy to do***ent etc, then you don't have to think about how
> much time to spend improving it.

Personally I think you're worrying unnecessarily, your first attempt
looks OK to me. When working with strings I often end up with a
BEGIN... WHILE...WHILE...REPEAT...THEN structure. The only thing that
grates is the THEN. I think it looks more complicated because of the
way you've laid it out.

Incidentally if you have a Forth that handles multiple local
declarations( I think GForth does) you can simplify your last attempt
by getting rid of the TO's e.g.

: GOOD-STRING2 { ca len xt }
   ca len
   BEGIN
      { ca' len' }
      len' 0 >
   WHILE
      ca' xt EXECUTE
   WHILE
      ca' len' 1 /STRING
   REPEAT
   THEN
   ca len len' -
;

Untested but I don't see why it wouldn't work, but probably isn't
F200X compliant. It also shows my preferred layout.

Gerry
 




 58 Posts in Topic:
what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-16 15:31:12 
Re: what kind of non microconroller app are done in forth?
pablo reda <pabloreda@  2008-04-16 15:57:50 
Re: what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-16 16:13:28 
Re: what kind of non microconroller app are done in forth?
stephenXXX@[EMAIL PROTECT  2008-04-17 09:26:43 
Re: what kind of non microconroller app are done in forth?
pablo reda <pabloreda@  2008-04-17 06:28:26 
Re: what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-18 10:11:10 
Re: what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-18 10:12:04 
Re: what kind of non microconroller app are done in forth?
Elizabeth D Rather <er  2008-04-18 08:49:22 
Re: what kind of non microconroller app are done in forth?
Helmar <helmwo@[EMAIL   2008-04-18 11:10:45 
Re: what kind of non microconroller app are done in forth?
pablo reda <pabloreda@  2008-04-18 12:04:14 
Re: what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-18 16:22:53 
Re: what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-18 16:24:09 
Re: what kind of non microconroller app are done in forth?
Elizabeth D Rather <er  2008-04-18 20:47:50 
Re: what kind of non microconroller app are done in forth?
jennifer.spykerman@[EMAIL  2008-04-19 04:15:42 
Re: what kind of non microconroller app are done in forth?
Duke Normandin <dukeof  2008-04-19 14:20:53 
Re: what kind of non microconroller app are done in forth?
Robert Spykerman <robe  2008-04-19 17:07:15 
Re: what kind of non microconroller app are done in forth?
Duke Normandin <dukeof  2008-04-20 02:50:54 
Re: what kind of non microconroller app are done in forth?
Elizabeth D Rather <er  2008-04-19 21:48:07 
Re: what kind of non microconroller app are done in forth?
Jonah Thomas <jethomas  2008-04-19 10:40:11 
Re: what kind of non microconroller app are done in forth?
Thomas Pornin <pornin@  2008-04-20 07:11:54 
Re: what kind of non microconroller app are done in forth?
Bruce McFarling <agila  2008-04-19 09:33:55 
Re: what kind of non microconroller app are done in forth?
Bruce McFarling <agila  2008-04-19 09:37:25 
Re: what kind of non microconroller app are done in forth?
Guy Macon <http://www.  2008-04-19 17:47:45 
Re: what kind of non microconroller app are done in forth?
Jonah Thomas <jethomas  2008-04-19 14:31:39 
Re: what kind of non microconroller app are done in forth?
Bruce McFarling <agila  2008-04-19 15:04:48 
Re: what kind of non microconroller app are done in forth?
Albert van der Horst <  2008-04-20 13:43:04 
Re: what kind of non microconroller app are done in forth?
Bruce McFarling <agila  2008-04-19 15:21:12 
Re: what kind of non microconroller app are done in forth?
Gerry <gerry@[EMAIL PR  2008-04-20 01:50:08 
Re: what kind of non microconroller app are done in forth?
Jonah Thomas <jethomas  2008-04-20 08:23:50 
Re: what kind of non microconroller app are done in forth?
Gerry <gerry@[EMAIL PR  2008-04-20 08:12:11 
Elegant code Was: Re: what kind of non microconroller app are d
Jonah Thomas <jethomas  2008-04-21 01:58:43 
Re: Elegant code
Josh Grams <josh@[EMAI  2008-04-21 11:56:34 
Re: Elegant code Was: Re: what kind of non microconroller app a
"David N. Williams&q  2008-04-21 12:12:22 
Re: Elegant code Was: Re: what kind of non microconroller app a
mhx@[EMAIL PROTECTED] (M  2008-04-21 19:57:21 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Gerry <gerry@[EMAIL PR  2008-04-21 02:15:31 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Gerry <gerry@[EMAIL PR  2008-04-21 03:53:49 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Jonah Thomas <jethomas  2008-04-21 09:04:09 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Elizabeth D Rather <er  2008-04-21 07:42:01 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Jonah Thomas <jethomas  2008-04-21 10:33:13 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Bruce McFarling <agila  2008-04-21 08:22:03 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Gerry <gerry@[EMAIL PR  2008-04-21 09:45:27 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Gerry <gerry@[EMAIL PR  2008-04-22 02:20:59 
Re: Elegant code Was: Re: what kind of non microconroller app ar
John Passaniti <nntp@[  2008-04-22 17:22:35 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Elizabeth D Rather <er  2008-04-22 14:16:18 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Ian Osgood <iano@[EMAI  2008-04-22 10:00:09 
Re: Elegant code Was: Re: what kind of non microconroller app ar
John Passaniti <nntp@[  2008-04-22 17:36:41 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Gerry <gerry@[EMAIL PR  2008-04-22 22:59:42 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Alex McDonald <blog@[E  2008-04-23 01:00:52 
Re: Elegant code Was: Re: what kind of non microconroller app ar
stephenXXX@[EMAIL PROTECT  2008-04-23 10:07:15 
C interface (was: Elegant code)
anton@[EMAIL PROTECTED]   2008-04-23 20:04:07 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Gerry <gerry@[EMAIL PR  2008-04-23 03:20:05 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Alex McDonald <blog@[E  2008-04-23 04:09:54 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Alex McDonald <blog@[E  2008-04-23 04:14:57 
Re: Elegant code Was: Re: what kind of non microconroller app ar
Jonah Thomas <jethomas  2008-04-23 10:11:34 
Re: Elegant code Was: Re: what kind of non microconroller app ar
John Passaniti <nntp@[  2008-04-23 20:56:59 
Re: what kind of non microconroller app are done in forth?
gavino <gavcomedy@[EMA  2008-04-29 00:16:19 
Re: what kind of non microconroller app are done in forth?
Elizabeth D Rather <er  2008-04-28 21:47:07 
Re: what kind of non microconroller app are done in forth?
Robert Spykerman <robe  2008-04-29 04:32:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Sat Jul 26 4:20:12 CDT 2008.