Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #49460

Re: Implementing binary search without going deeper than top 2 items on stack?

Subject Re: Implementing binary search without going deeper than top 2 items on stack?
References <5db2ff24-c31d-45a0-a4f9-6b0515203925@googlegroups.com> <2016Jul25.095102@mips.complang.tuwien.ac.at>
Newsgroups comp.lang.forth
From "Elizabeth D. Rather" <erather@forth.com>
Organization FORTH, Inc.
Date 2016-07-24 22:48 -1000
Message-ID <vsCdncXT4IXrTwjKnZ2dnUU7-RnNnZ2d@supernews.com> (permalink)

Show all headers | View raw


On 7/24/16 9:51 PM, Anton Ertl wrote:
> Alex Dowad <alexinbeijing@gmail.com> writes:
>> Dear comp.lang.forth,
>>
>> In his "1x Forth" talk (and other talks), Chuck Moore advocates for coding style whereby:
>>
>> 1. All definitions are at most 2 lines, and
>> 2. Words do not directly access anything deeper than the top 2 items on the stack
>>
>> In line with #2, Moore's colorForth has DUP, DROP, and OVER built in, but not PICK, ROLL, and so on.
>>
>> I'd like to see examples of how this can be practical. For example, how would you implement a binary
>> search without violating these rules?
>
> I think there are several answers typical of Chuck Moore style:
>
> 1) Implement it as CODE word.
>
> 2) On his Forth processors, where you cannot work around the
>    restriction with CODE words, he introduced address registers (A, R)
>    which allowed reducing the stack load; or seen the other way, it
>    allowed dealing with more values without violating the restriction.
>
> 3) Use the return stack.
>
> 4) Use global VARIABLEs.
>
> You can find various ways to reduce the stack load in the following
> paper:
>
> @InProceedings{ertl11euroforth,
>   author =       {M. Anton Ertl},
>   title =        {Ways to Reduce the Stack Depth},
>   crossref =     {euroforth11},
>   pages =        {36--41},
>   url =          {http://www.complang.tuwien.ac.at/papers/ertl11euroforth.ps.gz},
>   url2 =         {http://www.complang.tuwien.ac.at/anton/euroforth/ef11/papers/ertl.pdf},
>   OPTnote =      {not refereed},
>   abstract =     {Having to deal with many different data can lead to
>                   problems in Forth: The data stack is the preferred
>                   place to store data; on the other hand, dealing with
>                   too many data stack items is cumbersome and usually
>                   bad style. This paper presents and discusses ways to
>                   unburden the data stack; some of them are used
>                   widely, others are almost unknown or new.}
> }
>
> @Proceedings{euroforth11,
>   title = 	 {27th EuroForth Conference},
>   booktitle = 	 {27th EuroForth Conference},
>   year = 	 {2011},
>   key =		 {EuroForth'11},
>   url =          {http://www.complang.tuwien.ac.at/anton/euroforth/ef11/papers/proceedings.pdf}
> }
>
>> ("Directly accessing deeper items on the stack" would include using >R to get the top item(s) out of the way, then working with the deeper items.)
>
> No.  Forth has the words >R R> R@ for a reason.
>
>> Or is the answer "if you're using binary search, you're doing it wrong. Don't use binary search"?
>
> I don't know if Chuck Moore holds this opinion.  However, I find that
> I almost never actually use binary search.  I do consider it
> relatively often, but usually find a solution that works without it.

I recall that he wrote several binary searches back in the day, 
searching indexes of text keys in a database. Each identified key would 
include the record number of the data record associated with the key.

Chuck's definitions were sometimes more than 2 lines long; that was a 
desiderata, not an ultimatum. The context was the database package that 
was a feature of polyFORTH for a number of years, and searches worked on 
an index that was maintained in sorted order (insertions and deletions 
took longer, but searches were very fast).

I also must point out that Chuck had no reluctance to use global 
variables, and never understood the horror that some people seem to have 
of variables. Variables that needed to be task-specific were user 
variables, but there weren't that many of them; others were useful 
mechanisms for inter-task communication.

> BTW, I wonder why so many people who wonder what Chuck Moore would do
> don't care for Usenet conventions like limiting the line length to
> about 70 characters.

:-)

Cheers,
Elizabeth

-- 
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

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


Thread

Implementing binary search without going deeper than top 2 items on stack? Alex Dowad <alexinbeijing@gmail.com> - 2016-07-24 10:41 -0700
  Re: Implementing binary search without going deeper than top 2 items on stack? Paul Rubin <no.email@nospam.invalid> - 2016-07-24 11:18 -0700
  Re: Implementing binary search without going deeper than top 2 items on stack? Brad Eckert <hwfwguy@gmail.com> - 2016-07-24 13:23 -0700
    Re: Implementing binary search without going deeper than top 2 items on stack? Alex Dowad <alexinbeijing@gmail.com> - 2016-07-24 23:10 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? humptydumpty <ouatubi@gmail.com> - 2016-07-24 23:28 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? Julian Fondren <ayrnieu@gmail.com> - 2016-07-25 00:08 -0700
        Re: Implementing binary search without going deeper than top 2 items on stack? Alex Dowad <alexinbeijing@gmail.com> - 2016-07-25 00:29 -0700
          Re: Implementing binary search without going deeper than top 2 items on stack? Paul Rubin <no.email@nospam.invalid> - 2016-07-25 01:00 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? Brad Eckert <hwfwguy@gmail.com> - 2016-07-25 09:36 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? hughaguilar96@gmail.com - 2016-07-25 10:11 -0700
  Re: Implementing binary search without going deeper than top 2 items on stack? Alex Dowad <alexinbeijing@gmail.com> - 2016-07-25 01:10 -0700
    Re: Implementing binary search without going deeper than top 2 items on stack? humptydumpty <ouatubi@gmail.com> - 2016-07-25 01:32 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? humptydumpty <ouatubi@gmail.com> - 2016-07-25 09:48 -0700
        Re: Implementing binary search without going deeper than top 2 itemson stack? "WJ" <w_a_x_man@yahoo.com> - 2016-07-25 21:49 +0000
      Re: Implementing binary search without going deeper than top 2 items on stack? hughaguilar96@gmail.com - 2016-07-25 12:15 -0700
        Re: Implementing binary search without going deeper than top 2 items on stack? humptydumpty <ouatubi@gmail.com> - 2016-07-26 00:40 -0700
          Re: Implementing binary search without going deeper than top 2 items on stack? hughaguilar96@gmail.com - 2016-07-28 15:11 -0700
            Re: Implementing binary search without going deeper than top 2 items on stack? hughaguilar96@gmail.com - 2016-07-30 22:47 -0700
        Re: Implementing binary search without going deeper than top 2 items on stack? josv@planet.nl - 2016-07-26 01:53 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? mhx@iae.nl - 2016-07-30 21:31 -0700
        Re: Implementing binary search without going deeper than top 2 items on stack? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-07-31 05:49 +0000
          Re: Implementing binary search without going deeper than top 2 items on stack? mhx@iae.nl - 2016-07-31 00:17 -0700
            Re: Implementing binary search without going deeper than top 2 items on stack? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-07-31 13:13 +0000
  Re: Implementing binary search without going deeper than top 2 items on stack? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-07-25 07:51 +0000
    Re: Implementing binary search without going deeper than top 2 items on stack? "Elizabeth D. Rather" <erather@forth.com> - 2016-07-24 22:48 -1000
    Re: Implementing binary search without going deeper than top 2 items on stack? hughaguilar96@gmail.com - 2016-07-25 10:02 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? humptydumpty <ouatubi@gmail.com> - 2016-07-25 10:10 -0700
  Re: Implementing binary search without going deeper than top 2 items on stack? Alex Dowad <alexinbeijing@gmail.com> - 2016-07-26 22:56 -0700
  Re: Implementing binary search without going deeper than top 2 items on stack? lehs <skydda.barnen@gmail.com> - 2016-07-30 23:28 -0700
    Re: Implementing binary search without going deeper than top 2 items on stack? hughaguilar96@gmail.com - 2016-07-31 00:19 -0700
      Re: Implementing binary search without going deeper than top 2 items on stack? lehs <skydda.barnen@gmail.com> - 2016-07-31 00:31 -0700

csiph-web