EC is a useful command-line RPN calculator. It’s designed to be powerful and efficient from the command line.
EC’s design has several inspirations:
dc
, the reverse-polish desk calculator which comes standard on many UNIX systems;Taken together, EC tries to harmonize three powerful concepts:
⊕ ec 10 5 +
15
In Reverse-Polish Notation (or RPN), mathematical operations are expressed with the operator in the final place.
While this appears a little unfamiliar, it has some real
benefits. Chief among them: there is no concept of operator precedence
and no need for parentheses to indicate order of operations. There is
no ambiguity between 4 + 2 * 4
and (4 + 2) * 4
; those would be 2 4 * 4 +
and 2 4 + 4 *
(or 4 2 4 + *
) respectively. When writing
quick sums on the command line, it helps to not have to futz with
parenthesis and infix notation.
In addition to ordinary integer and floating point numbers, EC introduces Vectors, which are multi-dimensional lists of numbers. EC operators can work on vectors of different shape without any special notation. For example, here we see operations between two numbers, a number and a vector, two vectors, and vectors of differing shape:
ec 100 0.65 x
65
⊕ ec [3.15 3.20 3.50 3.65 3.85] 0.65 x
[2.0475 2.08 2.275 2.3725 2.5025]
⊕ ec [3.15 3.20 0.350] [0.65 0.80 0.75] x
[2.0475 2.56 0.2625]
⊕ ec [[3.15 3.20 0.350] [100 200 300]] [0.65 0.80 0.75] x
[[2.0475 2.56 0.2625] [65 160 225]]
Finally, a sequence of symbols can be wrapped in parentheses or curly
braces to form Quotations, which is unevaluated code. These
unevaluated programs can be manipulated by Combinators like
dist
, below, to perform powerful higher-level functional operations.
In this example, dist
inserts the quoted operation in between each
element of the vector. It’s trivial to construct a program to find the
sum of a list of numbers out of these primitives.
⊕ ec [2.5 100 2 14] 0 {+} dist
118.5
Operators that are manipulated by combinators are designed to remain fully compatible with array math, as in the following example:
⊕ ec [[2.5 100 2 14] [45 55 65 75] [100 99 98 97]] 0 {+} dist
[147.5 254 165 186]
EC inherits (lifts wholesale) from Joy many useful Combinators: programs which manipulate one or more quotation to accomplish powerful, complex operations. Here are som examples, along with descriptions from EC’s help system.
dist
⊕ ec {/} ?
--
dist: dist
S v a q -- S
Insert `a`, then apply quotation `q` after every element of `v`.
⊕ ec [2.5 100 2 14] 0 {+} dist
118.5
fork
⊕ ec {fork} ?
--
fork: fork
S a q p -- S a' a''
Apply q to a, and p to a, and push the results on the stack.
⊕ ec [34 20 12] {0 {+} /} {length} fork %
22
primrec
⊕ ec {primrec} ?
--
primrec: primrec
S d zq oq -- S
Primitive recursion.
> If the data parameter is [zero or empty], then the first quotation has to
> produce the value to be returned. If the data parameter is
> positive then the second has to combine the data parameter with
> the result of applying the function to its predecessor.
Manfred von Thun, _Tutorial on Joy_
⊕ ec 5 {1} {*} primrec
120