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


Groups > comp.lang.forth > #35806

Re: How to solve this problem in FORTH?

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: How to solve this problem in FORTH?
Date 2015-01-14 09:06 +0000
Organization NewsGuy - Unlimited Usenet $19.95
Message-ID <m95bjd02k4l@enews6.newsguy.com> (permalink)
References <f195a1a1-8609-4319-9df9-d8cbb2a11fdb@googlegroups.com> <d71058a5-aa4c-4308-acf3-346b610d6d8c@googlegroups.com>

Show all headers | View raw


mhx@iae.nl wrote:

> Marek Brunda <marekb@phoenics.org> writes 
> > How to solve this problem in FORTH? What do you thing about this post? http://prog21.dadgum.com/33.html
> 
> > My hurriedly solution is this:
> 
> [..]
> 
> > : ADD ( A1 A2 A3 U -- A1 A2 A3 )
> >    CELLS SWAP >R >R
> >    2DUP R@ + @ 
> >    SWAP R@ + @ +
> >    R> R@ + ! R> ;    
>     
> > : VADD ( A1 A2 A3 -- )
> >   0 ADD 1 ADD 2 ADD 2DROP DROP ;
> 
> Not on every Forth of course, but I would try this:
> 
> FORTH> : vadd  params| a b c |
>   a          @  b          @  +  c          !
>   a    cell+ @  b   cell+  @  +  c    cell+ !
>   a 2 cell[] @  b 2 cell[] @  +  c 2 cell[] ! ;  ok
> 

That awkward, inelegant, tedious to write, tedious to read
"solution" only works for vectors of length 3.

Gauche Scheme:

Creates a new vector:

(define (v-add v1 v2)
  (vector-map + v1 v2))


Changes an existing vector:

(define (v-add! v1 v2 v3)
  (vector-map!
    (lambda (_ val1 val2) (+ val1 val2))
    v3 v1 v2))


Testing:

(define v1 #(10 20 30 40 50 60 70 80 90))
(define v2 #(100 200 300 400 500 600 700 800 900))
(define v3 #(0 0 0 0 0 0 0 0 0))

(v-add v1 v2)
 ===>
#(110 220 330 440 550 660 770 880 990)


(v-add! v1 v2 v3)
v3
 ===>
#(110 220 330 440 550 660 770 880 990)


Let's write a version that will add up any number
of vectors:

(define (v-add* . vectors)
  (apply vector-map + vectors))

(v-add* v1 v2)
 ===>
#(110 220 330 440 550 660 770 880 990)

(v-add* v1 v2 v3)
 ===>
#(220 440 660 880 1100 1320 1540 1760 1980)



Let's write a version that will add up any number
of vectors and store the result in a vector:

(define (v-add*! result . vecs)
  (apply vector-map!
    (lambda args (apply + (cdr args)))
    result vecs))

Testing:

(define v4 #(0 0 0 0 0 0 0 0 0))

(v-add*! v4  v1 v2 v3)
v4
 ===>
#(220 440 660 880 1100 1320 1540 1760 1980)



Too easy?  Let's do it in EMACS Lisp.

(defun v-add! (result &rest vecs)
  (dotimes (i (length (car vecs)))
    (aset result i
      (apply '+ (mapcar (lambda (v) (aref v i)) vecs)))))

Testing:

(setq v1 [10 20 30 40 50 60 70 80 90])
(setq v2 [100 200 300 400 500 600 700 800 900])
(setq v3 [1000 2000 3000 4000 5000 6000 7000 8000 9000])
(setq v4 (make-vector 20 0))

(v-add! v4  v1 v2 v3)
v4
 ===>
[1110 2220 3330 4440 5550 6660 7770 8880 9990 0 0 0 0 0 0 0 0 0 0 0]

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


Thread

How to solve this problem in FORTH? Marek Brunda <marekb@phoenics.org> - 2015-01-13 04:24 -0800
  How to solve this problem in FORTH? Julian Fondren <julian.fondren@gmail.com> - 2015-01-13 05:02 -0800
    How to solve this problem in FORTH? Julian Fondren <julian.fondren@gmail.com> - 2015-01-13 05:09 -0800
  Re: How to solve this problem in FORTH? november.nihal@gmail.com - 2015-01-13 05:29 -0800
    Re: How to solve this problem in FORTH? "WJ" <w_a_x_man@yahoo.com> - 2015-01-14 14:00 +0000
      Re: How to solve this problem in FORTH? november.nihal@gmail.com - 2015-01-14 06:59 -0800
        Re: How to solve this problem in FORTH? awegel@arcor.de (Alexander Wegel) - 2015-01-14 23:39 +0100
  Re: How to solve this problem in FORTH? Coos Haak <chforth@hccnet.nl> - 2015-01-13 15:12 +0100
  Re: How to solve this problem in FORTH? humptydumpty <ouatubi@gmail.com> - 2015-01-13 10:30 -0800
  Re: How to solve this problem in FORTH? mhx@iae.nl - 2015-01-13 12:14 -0800
    Re: How to solve this problem in FORTH? "WJ" <w_a_x_man@yahoo.com> - 2015-01-14 09:06 +0000
      Re: How to solve this problem in FORTH? mhx@iae.nl - 2015-01-14 04:13 -0800
      Re: How to solve this problem in FORTH? Julian Fondren <julian.fondren@gmail.com> - 2015-01-14 04:55 -0800
      Re: How to solve this problem in FORTH? "WJ" <w_a_x_man@yahoo.com> - 2015-03-27 06:47 +0000
  Re: How to solve this problem in FORTH? jrcd83@gmail.com - 2015-03-28 08:09 -0700
    Re: How to solve this problem in FORTH? "Alex McDonald" <blog@rivadpm.com> - 2015-03-28 20:53 +0000
  Re: How to solve this problem in FORTH? rickman <gnuarm@gmail.com> - 2015-03-28 20:32 -0400
  Re: How to solve this problem in FORTH? hughaguilar96@gmail.com - 2015-03-28 21:16 -0700
    Re: How to solve this problem in FORTH? hughaguilar96@gmail.com - 2015-03-29 12:16 -0700

csiph-web