## [REBOL] Re: Thanks for the warm welcome!

### From: brian::hawley::net at: 27-Apr-2001 12:26

Hi Tom!
At 05:45 PM 4/29/01 -0500, you wrote:

>Thank you all for your welcome and support.
>
> I took Carl's advice and studied series for the last few days. It looked
>like it might be fun to recreate some APL, a math language, just to get
>something going.
>
>So here is my contribution. If you know of a way to tighten up the code,
>please don't hesitate to let me know.
>
>Also the function in APL for summing a vector or array is '+/' but I had
>trouble defining that as a function, would sure appreciate some suggestions.

REBOLs typically give their functions names rather than symbols :)
Here's my suggestions, using more natives. If you want the sum
or product of empty blocks to be none rather than 0, just leave
the word total off the end of the function, letting the foreach
be the last expression.

>REBOL [
> Title: "Math Functions"
> Author: "Tom Schaeper"
> Date: 26-Apr-2001
> Version: .5
>]
>
>v+: func [
> "Sum a block of values"
> block1 [block!] "Block of numbers passed"
> ][
> block1: head block1
> total: 0
> forall block1 [ total: total + block1/1]
>
> return total
> ]

sum: func [
"Sum a block of values"
vec [block!] "Block of numbers passed"
/local total
] [
total: 0
foreach x head vec [total: total + x]
total
]

>v*: func [
> "Sum a block of values"
> block1 [block!] "Block of numbers passed"
> ][
> block1: head block1
> total: 1
> forall block1 [ total: total * block1/1]
>
> return total
> ]

product: func [
"Generate the product of a block of values"
vec [block!] "Block of numbers passed"
/local total
] [
total: 0
foreach x head vec [total: total * x]
total
]

>viota: func [
> "Generate a block of integers from 1 to num"
> num [integer!] "Number of entries from 1 to num"
> ][
> i: 0
> block1: copy []
> while [ i < num] [
> i: i + 1
> block1: insert block1 i
> ]
> block1: head block1
> ]

vec-iota: func [
"Generate a block of integers from 1 to num"
num [integer!] "Number of entries from 1 to num"
/local block
] [
; Preallocate the result block
block: make block! num
; Let the repeat native do your calculations
repeat x num [block: insert block x]
; The last assignment isn't necessary
head block
]
These should be somewhat faster. That was fun...
Brian Hawley