[REBOL] More on expressions
From: joel::neely::fedex::com at: 1-Feb-2003 10:21
Inspired by the discussions of ANY and ALL, I thought I'd share this
little puzzle, prefaced with a tad of background.
Some languages are described as "imperative" or "procedural" because
the programmer is writing commands to be obeyed. PASCAL, COBOL,
FORTRAN, and (the way most published code appears) C come to mind.
Other languages are designated as "functional" because the programmer
is defining functions whose (side-effect-free) evaluation yields the
desired result. SCHEME, ML, and HASKELL come to mind.
I think of REBOL as a hybrid which I'll call "expressional" because
we write expressions to be evaluated, but often the (side-)effect
of those evaluations is the goal. For instance APPEND has both a
resulting value and an effect; sometimes we want one, or the other,
or both.
Thinking clearly about what our expressions can do for us is a very
stimulating way to explore the power of programming concepts. Here
is a little puzzle along that vein.
GIVEN: Three variables, A, B, and C which we'll assume have
been set to numeric values.
CHALLENGE: Write an expression whose value is the median of the
values. A trival (but time-consuming) solution is
second sort reduce [a b c]
since the median of a set of values is "the one in the
middle if they are in order".
CONSTRAINT: Just to make it iteresting ;-) the expression MAY NOT
use any of these REBOL words/functions:
sort if either first second third
nor any path expressions.
The trick, of course, is to find an expression that eliminates the
need for any explicit "decision" or "selection" activity.
Have fun!
-jn-