[REBOL] Re: Clunky-looking code
From: jelinem1:nationwide at: 2-Apr-2001 12:51
To write the most compact piece of code for performing this task one could
split some hairs and chop out a few chars here and there, but that does not
necessarily make it more readable/maintainable or better.
Despite the fact that this email will get like a zillion replies, here's my
2c. If you want to multiply two blocks, once, and have no need to get into
this sort of thing any more deeply, put your inelegant code in a function
(where it can elegantly be called) and get on with it. It you wish to use
the concept over again, and even expound on it, write a dialect. The
dialecting is the part that REBOL will excel at over other languages.
- Michael Jelinek
[Sanghabum--aol--com]@rebol.com on 04/02/2001 12:25:11 PM
From: [Sanghabum--aol--com]@rebol.com on 04/02/2001 12:25 PM
Please respond to [rebol-list--rebol--com]
Sent by: [rebol-bounce--rebol--com]
To: [rebol-list--rebol--com]
cc:
Subject: [REBOL] Clunky-looking code
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.