[REBOL] Clunky-looking code
From: sanghabum::aol::com at: 2-Apr-2001 13:25
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
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!
Am I missing some elegant way of running up two blocks in step?
;; ----- Method 1
MultiplyBlocks1: func [Block1 Block2 /local Sum ind]
[
sum: 0
for ind 1 Length? Block1 1
[
sum: sum + ((pick Block1 ind) * (pick Block2 ind))
] ; for
return sum
] ; func
;; ----- Method 2
MultiplyBlocks2: func [Block1 Block2 /local Sum]
[
sum: 0
Loop (Length? Block1)
[
sum: sum + ((first Block1) * (first Block2))
block1: Next Block1
block2: Next Block2
] ; loop
Return Sum
] ; func
;; ----- Example of execution
print MultiplyBlocks1 [1 5 10 25] [0 5 6 20]
print MultiplyBlocks2 [1 5 10 25] [0 5 6 20]
Thanks,
Colin.