Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.forth > #23410

Re: Programming Style

From rickman <gnuarm@gmail.com>
Newsgroups comp.lang.forth
Subject Re: Programming Style
Date 2013-06-10 18:56 -0400
Organization A noiseless patient Spider
Message-ID <kp5lbe$ikn$1@dont-email.me> (permalink)
References <kp2hhp$ktf$2@dont-email.me> <d714a5f9-974c-471f-97bd-624ea69d60df@googlegroups.com> <kp4pru$asq$1@dont-email.me> <1e754e31-4d33-4a6c-bd23-a422c1abf9bd@googlegroups.com>

Show all headers | View raw


On 6/10/2013 2:09 PM, humptydumpty wrote:
> On Monday, June 10, 2013 6:08:05 PM UTC+3, rickman wrote:
>> On 6/10/2013 3:31 AM, humptydumpty wrote:
>>
>>> On Sunday, June 9, 2013 9:33:52 PM UTC+3, rickman wrote:
>>
>>>> I'm interested in what others think of the style of these two approaches
>>
>>>>
>>
>>>> to the below function.  The purpose is to be a work around to a comms
>>
>>>>
>>
>>>> problem that I don't want to fix just yet.  I'd rather work on making
>>
>>>>
>>
>>>> the code more robust in handling comms errors.  In the meantime I want
>>
>>>>
>>
>>>> be able to use the code with minimal problems caused by the errors.
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> The below function queries a register in the UUT and repeats the test
>>
>>>>
>>
>>>> until either two reads match or the max count is reached.  I'm curious
>>
>>>>
>>
>>>> as to which approach seems better.  In the first, the loop is terminated
>>
>>>>
>>
>>>> when two reads match.  Not complex, but I learned "structured
>>
>>>>
>>
>>>> programming" in college and this sort of thing just doesn't "feel" right
>>
>>>>
>>
>>>> (maybe I should get over that).  The second continues in the loop doing
>>
>>>>
>>
>>>> nothing until the max count is reached, but is very clear as to what is
>>
>>>>
>>
>>>> happening.
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> It is not a big deal, I was just curious how others approach this or if
>>
>>>>
>>
>>>> there are other, better ways of dealing with this?
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> BTW, I haven't tested either of these definitions... as yet.
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> : RepeatAmpRead ( n -- u ) \ input max iterations
>>
>>>>
>>
>>>>       -2 SWAP 0 ?DO
>>
>>>>
>>
>>>>          TFAmpMeasRslt0Addr RdRegQuadCmd		( old new )
>>
>>>>
>>
>>>>          2DUP = IF NIP FALSE LEAVE THEN		( val | old new )
>>
>>>>
>>
>>>>          CR ." RepeatAmpRead - " I . SWAP H. DUP H. ( new )
>>
>>>>
>>
>>>>          TRUE
>>
>>>>
>>
>>>>       LOOP
>>
>>>>
>>
>>>>       IF CR ." 	>>>ERROR<<<   Measurement value not stable" .s THEN
>>
>>>>
>>
>>>>       ;
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> : RepeatAmpRead ( n -- u ) \ input max iterations
>>
>>>>
>>
>>>>       -2 FALSE ROT 0 ?DO \ Start old value at -2 and flag at FALSE
>>
>>>>
>>
>>>>          IF TRUE                                           ( val flag )
>>
>>>>
>>
>>>>          ELSE
>>
>>>>
>>
>>>>             TFAmpMeasRslt0Addr RdRegQuadCmd		( old new )
>>
>>>>
>>
>>>>             SWAP OVER					( new old new )
>>
>>>>
>>
>>>>             CR ." RepeatAmpRead - " I . OVER H. DUP H.	( new old new )
>>
>>>>
>>
>>>>             =                                              ( new flag )
>>
>>>>
>>
>>>>          THEN
>>
>>>>
>>
>>>>       LOOP
>>
>>>>
>>
>>>>       0= IF CR ." 	>>>ERROR<<<   Measurement value not stable" THEN
>>
>>>>
>>
>>>>       ;
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> --
>>
>>>>
>>
>>>>
>>
>>>>
>>
>>>> Rick
>>
>>>
>>
>>> Hi!
>>
>>>
>>
>>> Other way:
>>
>>>
>>
>>> 8<---
>>
>>> : Reads@        ( -- old new )
>>
>>>           TFAmpMeasRslt0Addr RdRegQuadCmd
>>
>>> ;
>>
>>> : (reads)       ( n i -- n i old new )
>>
>>>           BEGIN
>>
>>>                   Reads@ 2dup<>
>>
>>>           WHILE
>>
>>>                   2over<>
>>
>>>           WHILE
>>
>>>                   2drop 1+
>>
>>>           REPEAT
>>
>>>           THEN
>>
>>> ;
>>
>>> : reads         ( n -- u f )
>>
>>>           0 (reads) drop rot rot -
>>
>>> ;
>>
>>> --->8
>>
>>>
>>
>>> Have a nice day,
>>
>>> humptydumpty
>>
>>
>>
>> I can't see where this does the same thing.  After Reads@ there should
>>
>> be three things on the stack and the top two are duped and compared then
>>
>> the the comparison result is removed by the while leaving three things
>>
>> on the stack.  The 2over<>  would be accessing something below the top
>>
>> three items on the stack possibly causing an underflow.  No?  Oh, I see,
>>
>> the Reads@ only does one read, not two.  That is why I init with a -2,
>>
>> that's the reference that is highly unlikely to show up as a match to
>>
>> the next read.  I should have explained what TFAmpMeasRslt0Addr and
>>
>> RdRegQuadCmd are.  TFAmpMeasRslt0Addr is a constant for the address and
>>
>> RdRegQuadCmd reads four 8 bit registers combining them into a single 32
>>
>> bit result.
>>
>>
>>
>> I don't get what the 2over<>  is intended to accomplish.  It seems it is
>>
>> checking the loop count, but how?  Oh, I see, it is expecting n and i
>>
>> where i is incremented.  But at the bottom of the loop aren't both
>>
>> dropped?  This is the sort of hybrid structure I was thinking of but I
>>
>> can't say I like it much.  I believe the second WHILE will branch to
>>
>> between the REPEAT and the THEN.  The first WHILE branches to after the
>>
>> THEN, no?
>>
>>
>>
>> I was thinking more of something like
>>
>>
>>
>> BEGIN
>>
>>     ...
>>
>> WHILE
>>
>>     ...
>>
>> UNTIL
>>
>>     ...
>>
>> THEN
>>
>> --
>>
>>
>>
>> Using the UNTIL for the loop count would allow for the error handling
>>
>> just before the THEN.  I would want to document this clearly.  I'm no
>>
>> forth pro and would not remember this clearly in a few months.
>>
>>
>>
>> Rick
>
> Hi Rick!
>
> Using flag to signal success was that kicked me to posting.
> I think we could do the job without flag.
>
> After clarifications I've got from you:
>
> : Read@  ( -- value )
>    ...
> ;
>
> \ After (reads) exit we will have *all* information on stack
>
> : (reads)  ( reads-limit count value -- reads-limit count old-value new-value )
>     BEGIN
>         Read@ 2dup<>
>     WHILE                   ( lim cnt old new )
>         2over<>
>     WHILE                   ( lim cnt old new )
>         nip 1 under+        ( lim cnt+1 new   )
>     REPEAT
>     THEN
> ;
>
> \ Example of usage:
>
> : reads    ( reads-limit -- last-value till-end  ;till-end=0 means failure)
>    1 -2 (reads) nip -rot -
> ;
>
> I have tried to isolate loop in one minimalist word,
> exiting earlier as possible without information loss.
>
> Of course we can decorate "reads" with printing of exit status.
>
>> I'm no forth pro and would not remember this clearly in a few months.
>
> Me too :)
>
> Have a nice day,
> humptydumpty

Ok thanks.  I got away from using the count to flag the success or not 
because it wastes the last read.  Instead the loop count is only used to 
exit the loop if all the reads fail and don't match.  In your case 
exiting on a final match still gets flagged as a failure because the 
loop count is the same as if it really did fail the final read.

At the end instead of the nip -rot - it would be better to 2NIP SWAP 
OVER = with a TRUE indicating success.  But your isolated loop is a 
great idea, easy to test.  I tend to program functionally sometimes 
having words that aren't so easy to test.

What exactly is the significance of using parens on (reads)?  I've seen 
this before, but don't recall why they are used.  I guess it indicates 
an internal word in some sense?  You can tell by the names I use that I 
am from a C background... lol

-- 

Rick

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Programming Style rickman <gnuarm@gmail.com> - 2013-06-09 14:33 -0400
  Re: Programming Style Paul Rubin <no.email@nospam.invalid> - 2013-06-09 11:42 -0700
    Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-09 15:22 -0400
  Re: Programming Style "WJ" <w_a_x_man@yahoo.com> - 2013-06-09 19:16 +0000
    Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-09 15:36 -0400
      Re: Programming Style Roberto Waltman <usenet@rwaltman.com> - 2013-06-09 22:17 -0400
    Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-22 09:53 -0400
  Re: Programming Style humptydumpty <ouatubi@gmail.com> - 2013-06-10 00:31 -0700
    Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-10 11:08 -0400
      Re: Programming Style humptydumpty <ouatubi@gmail.com> - 2013-06-10 11:09 -0700
        Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-10 18:56 -0400
          Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-10 15:31 -1000
            Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-11 23:10 -0400
  Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-10 08:32 +0000
    Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-10 11:12 -0400
      Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-10 17:22 +0000
      Re: Programming Style Mark Wills <markrobertwills@yahoo.co.uk> - 2013-06-11 01:01 -0700
        Re: Programming Style Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2013-06-11 15:51 +0100
          Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-12 11:14 -0400
            Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-14 13:03 -1000
              Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-14 19:22 -0400
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-14 16:54 -1000
                Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-15 12:14 -0400
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-15 11:57 -1000
                Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-17 16:12 +0000
                Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-17 23:08 -0400
        Re: Programming Style Hannu Vuolasaho <hannu.vuolasaho@nospam.tut.fi.invalid> - 2013-06-11 18:42 +0000
          Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-12 12:00 +1000
            Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-11 17:07 -1000
              Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-13 14:21 +1000
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-12 18:35 -1000
                Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-13 16:56 +1000
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-12 21:03 -1000
                Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-14 10:20 +1000
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-13 15:46 -1000
                Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-15 23:27 +1000
                Re: Programming Style Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-06-15 10:53 -0500
                Re: Programming Style Mark Wills <markrobertwills@yahoo.co.uk> - 2013-06-14 01:34 -0700
                Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-14 12:12 +0000
                Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-14 15:13 +0000
                Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-17 21:25 +1000
                Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-17 15:37 +0000
                Re: Programming Style Elizabeth D Rather <erather@forth.com> - 2013-06-17 08:17 -1000
                Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-17 19:47 +0000
                Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-18 08:12 +0000
                Re: Programming Style Alex McDonald <blog@rivadpm.com> - 2013-06-17 12:59 -0700
                Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-17 23:33 -0400
                Re: Programming Style Hugh Aguilar <hughaguilar96@yahoo.com> - 2013-06-19 19:50 -0700
                Re: Programming Style Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-06-20 03:08 -0500
                Re: Programming Style Alex McDonald <blog@rivadpm.com> - 2013-06-20 01:55 -0700
                Re: Programming Style Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-06-20 05:10 -0500
                Re: Programming Style Bernd Paysan <bernd.paysan@gmx.de> - 2013-06-20 13:02 +0200
                Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-20 15:40 +0000
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-20 07:21 -1000
                Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-20 09:50 +0000
                Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-14 18:50 -0400
                Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-16 00:31 +1000
                Re: Programming Style Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-06-15 10:59 -0500
                Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-13 10:48 +0000
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-13 07:44 -1000
                Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-15 16:31 +0000
            Re: Programming Style albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-06-12 11:10 +0000
              Re: Programming Style mhx@iae.nl (Marcel Hendrix) - 2013-06-12 20:51 +0200
            Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-12 15:06 +0000
              Re: Programming Style "Ed" <invalid@invalid.com> - 2013-06-13 14:54 +1000
                Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-12 20:43 -1000
                Re: Programming Style anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-06-14 12:45 +0000
          Re: Programming Style rickman <gnuarm@gmail.com> - 2013-06-14 16:39 -0400
            Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-14 11:37 -1000
        Re: Programming Style Hugh Aguilar <hughaguilar96@yahoo.com> - 2013-06-11 18:23 -0700
      Re: Programming Style "Elizabeth D. Rather" <erather@forth.com> - 2013-06-15 08:45 -1000

csiph-web