Re: balanced REDUCE: a challenge for the brave



On 29 May, 11:51, Coos Haak <chfo...@xxxxxxxxx> wrote:
Op Thu, 29 May 2008 03:42:03 -0700 (PDT) schreef Gerry:





On 29 May, 11:19, Coos Haak <chfo...@xxxxxxxxx> wrote:
Op Thu, 29 May 2008 02:26:36 -0700 (PDT) schreef Gerry:

On 29 May, 06:10, Robert Spykerman <robert.spyker...@xxxxxxxxx> wrote:

[snip]

Ok I cheated with a global, but here's something that's at least
associative. Does this work for you ?

\ using recursion and deferred xt

defer (reducext)

: (reduce)
    dup 0= if
      \  do nothing if stack is 0 --
      \ end recurse
    else
        over 0= if
             \ end recurse
             \ do nothing if stack is 0 x --
        else
           (reducext)
           >R recurse R>
        then
    then
;

: reduce   \ 0 x-n x-1 x xt -- result
    is (reducext)
    begin over while
       (reduce)
            .S   \ add optional stack inspection here
       repeat
       nip
;

If there was something below the 0 at the bottom of the list on the
stack, (reduce) can be simplified to:

: (reduce)
   2dup * if

\ Strange, what if x is 256 on a 16 bit or 65536 on a 32 bit CPU?

Dammit, Google Groups just misfired on me. I meant to continue that
    2dup m* or
should work.

\ Why not simply
     dup 0= if

because it's tring to filter out ( 0 ) and ( 0 x )

Gerry

Ha, but then the following would work too:
  2dup and 0= if

No it wouldn't, if, say, the stack was ( 7 8 ) then
2dup and
gives 0 which is not what is wanted.

Gerry


I would not use multiplication, as that might be slow, more
so as REDUCE is recursive ;-)

--
Coos

CHForth, 16 bit DOS applicationshttp://home.hccnet.nl/j.j.haak/forth.html- Hide quoted text -

- Show quoted text -

.



Relevant Pages