## [REBOL] Re: Clunky-looking code

### From: brett:codeconscious at: 3-Apr-2001 10:20

> I'm trying to multiply respective elements of two blocks and get a single

sum:

> e.g.
> [a b c] * [d e f] = a*d + b*e + c*f
>

As you describe, you have two operations
1) Multiply the respective elements of two series.
2) Sum a series

> I have no problem writing the code .... There are two versions of the
> function listed below. But it looks irredeemably inelegant compared to

almost

> any other language I know .... I would not like to have to do matrix
> operations in Rebol!

I don't think Rebol was optimised as a language for doing matrix operations
and as far as I remember is no worse than any other language I know for this
type of operation - except for APL which I only experiemented in briefly.
As Michael Jelinek points out the best way to make it look elegant is to
code up your functions and call them. Functions effectively extend the
language to make it more expressive and perhaps elegant.
If you could assume things about your blocks then you can do the following:
vector-multiply: function [a b][result][
if not equal? length? a length? b [throw "Only accepts vectors of
the same size."]
result: array reduce [length? a]
for i 1 length? a 1 [
poke result i multiply a/:i b/:i
]
RETURN result
]
vector-sum: function [a][result][
result: 0
for i 1 length? a 1 [result: add result a/:i]
RETURN result
]
And therefore more elegantly code in you main function:
vector-sum vector-multiply [2 3 5] [3 5 7]
Next option would be to use Ladislav's %highfun.r script
(

http://www.rebol.org/advanced/index.html). There you will find functions
that will make this work more elegant, or a technique to make the functions
I've shown more generic.
Last alternative is to ask RT really really nicely if they would consider
changing the semantics of "multiply" in order to accept two blocks and of
add
to accept a block to sum. But they could only go so far - at some
point such changes will conflict with pre-existing functionality/semantics.
Brett.
---
http://www.codeconscious.com