Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #20376
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: SP-Forth: reduce or fold a list |
| Date | 2013-03-06 23:50 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <kh8knb$19b$1@dont-email.me> (permalink) |
| References | <kh80t5$ec8$1@dont-email.me> <kh8j7q$q72$1@dont-email.me> |
WJ wrote:
> WJ wrote:
>
> > REQUIRE list-all ~ygrek/lib/list/all.f
> > list ALSO!
> > REQUIRE RANDOMIZE ~af/lib/random.f
> >
> > 0 VALUE biglist
> >
> > : reduce-test
> > \ Build list of 9999 random integers.
> > %[ 9999 0 DO 5000 CHOOSE % LOOP ]%
> > TO biglist
> > S" Length of list is " TYPE biglist length .
> > \ Sum the list.
> > 0 biglist ['] + iter
> > CR S" Sum of list is " TYPE .
> > biglist free
> > ;
> >
> > reduce-test
> > Length of list is 9999
> > Sum of list is 25009246 Ok
>
>
> Factor:
>
> USING: random ;
>
> 9999 iota [ drop 5000 random ] map
> "Length of list is " write dup length .
> 0 [ + ] reduce
> "Sum of list is " write .
>
> Length of list is 9999
> Sum of list is 25051634
"reduce" isn't even needed; one can simply use "each"
(equivalent to "iter" in the SP-Forth code):
0 { 1 3 5 7 } [ + ] each .
16
Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread
SP-Forth: reduce or fold a list "WJ" <w_a_x_man@yahoo.com> - 2013-03-06 18:11 +0000
Re: SP-Forth: reduce or fold a list "Charles Childers" <crc@retroforth.org> - 2013-03-06 16:42 -0500
Re: SP-Forth: reduce or fold a list "WJ" <w_a_x_man@yahoo.com> - 2013-03-06 23:24 +0000
Re: SP-Forth: reduce or fold a list "WJ" <w_a_x_man@yahoo.com> - 2013-03-06 23:50 +0000
csiph-web