On Sun, Aug 3, 2008 at 6:30 PM, Andrew Lentvorski <[EMAIL PROTECTED]> wrote:
> This place here has an interesting discussion as to how to do a simple
> vector addition in Forth and what that entails:
>
> http://prog21.dadgum.com/33.html

This is not even wrong it is so bad.

First of all he should define vadd as taking two vectors, v1 and v2
from the stack and putting the addition v3 back onto the stack, i.e.

: vadd ( v1 v2 --- v3 )

where
     v1 puts a b c on the stack
     v2 puts d e f on the stack
     v3 is g h i where g = a+d, h = b+e and i=c+f

Here is an untested version. Normally I would test as I go but I do
not have Forth on this machine and am too lazy this evening to
download one.

Good Forth programs rarely are working with more than three or four
items on the stack. More than that and you are usually doing it the
wrong way. So the strategy is to move items off the parameter stack
until you have whittled it down to size.


: vadd ( a b c d e f --- g h i  )
             >R >R >R      ( d e f --- )      (R    c b a --- )
             rot                  ( e f d --- )      (R    c b a --- )
             rot                  ( f d e --- )      (R    c b a --- )
             R>                  ( c f d e --- )   (R    b a --- )
             +                    ( i d e --- )      (R    b a --- )
             rot                   ( d e i --- )     (R    b a --- )
             rot                   ( e i d --- )     (R    b a --- )
             R>                   ( b e i d --- )  (R    a --- )
             +                     ( h i d --- )     (R    a --- )
             rot                   ( i d h --- )     (R    a --- )
             rot                   ( d h i --- )     (R    a --- )
             R>                   ( a d h i --- )  (R    --- )
             +                     ( g h i --- )
;

So it goes.

It becomes a bit simpler with
: -rot ( a b c --- c a b ) rot rot ;

Further v1 and v2 should probably be defined using <BUILDS DOES>
and then simple accessors pull up the vector components a, b, c.

Sigh. With "experts" like this it is no wonder Forth gets a bad rap.

I am very tired having spent an hour and a half waiting in the hot sun
to cross thge border for a meeting today.

Any time you start needing all of those deep picks into a stack you
can bet your bippy you just do not get it. The whole reason Forth
works well and can be easy to write and debug is because you rarely
need to write a function with more than a few variables or more than a
few lines. You do need to write a lot of functions and think very
closely about how objects can be decomposed.

Most programmers do not think that  closely.

On that subject see "The very confused"
http://weblog.lowpro.ca/wp-content/uploads/2007/08/venn_diagram_example.gif

BobLQ

What about vectors with more components? Probably use pointers, something
like this.

: vnadd (  A B -- C )
      over over              ( A B A B --- )
      @ swap @ swap   ( [A] [B] A B --- )
       + >R                   ( A B --- )                       (R   [C] --- )
      1 + swap 1 +       ( A+1 B+1 --- )
       over over            ( A+1 B+1 A+1  B+1 --- )
       @ swap @ swap  ( [A+1] [B+1] A+1 B+1 --- )
       + >R                   ( A+1 B+1 --- )              (R   [C+1] [C] --- )
        :
        :
        This puts them on the return stack.  Then you need to pull
         them off and use them or put them somewhere.
       + >R                   ( A B --- )


This obviously is leading toward a few primitives
: 2dup over over ;
: inc 1+ swap 1+ ;
: 2@ @ swap @ swap ;

>From which
: bump 2dup inc 2@ ;

etc ...

-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to