Next: Mapping, Previous: Reducing and Mapping, Up: Reducing and Mapping

#### 10.8.1 Specifying Operators

Commands in this section (like `V A`) prompt you to press the key corresponding to the desired operator. Press `?` for a partial list of the available operators. Generally, an operator is any key or sequence of keys that would normally take one or more arguments from the stack and replace them with a result. For example, `V A H C` uses the hyperbolic cosine operator, `cosh`. (Since `cosh` expects one argument, `V A H C` requires a vector with a single element as its argument.)

You can press `x` at the operator prompt to select any algebraic function by name to use as the operator. This includes functions you have defined yourself using the `Z F` command. (See Algebraic Definitions.) If you give a name for which no function has been defined, the result is left in symbolic form, as in ‘`f(1, 2, 3)`’. Calc will prompt for the number of arguments the function takes if it can't figure it out on its own (say, because you named a function that is currently undefined). It is also possible to type a digit key before the function name to specify the number of arguments, e.g., `V M 3 x f <RET>` calls `f` with three arguments even if it looks like it ought to have only two. This technique may be necessary if the function allows a variable number of arguments. For example, the `v e` [`vexp`] function accepts two or three arguments; if you want to map with the three-argument version, you will have to type `V M 3 v e`.

It is also possible to apply any formula to a vector by treating that formula as a function. When prompted for the operator to use, press `'` (the apostrophe) and type your formula as an algebraic entry. You will then be prompted for the argument list, which defaults to a list of all variables that appear in the formula, sorted into alphabetic order. For example, suppose you enter the formula ‘`x + 2y^x`’. The default argument list would be ‘`(x y)`’, which means that if this function is applied to the arguments ‘`[3, 10]`’ the result will be ‘`3 + 2*10^3`’. (If you plan to use a certain formula in this way often, you might consider defining it as a function with `Z F`.)

Another way to specify the arguments to the formula you enter is with `$`, `$$`, and so on. For example, `V A ' $$ + 2$^$$` has the same effect as the previous example. The argument list is automatically taken to be ‘`($$ $)`’. (The order of the arguments may seem backwards, but it is analogous to the way normal algebraic entry interacts with the stack.)

If you press `$` at the operator prompt, the effect is similar to the apostrophe except that the relevant formula is taken from top-of-stack instead. The actual vector arguments of the `V A $` or related command then start at the second-to-top stack position. You will still be prompted for an argument list.

A function can be written without a name using the notation ‘`<#1 - #2>`’, which means “a function of two arguments that computes the first argument minus the second argument.” The symbols ‘`#1`’ and ‘`#2`’ are placeholders for the arguments. You can use any names for these placeholders if you wish, by including an argument list followed by a colon: ‘`<x, y : x - y>`’. When you type `V A ' $$ + 2$^$$ <RET>`, Calc builds the nameless function ‘`<#1 + 2 #2^#1>`’ as the function to map across the vectors. When you type `V A ' x + 2y^x <RET> <RET>`, Calc builds the nameless function ‘`<x, y : x + 2 y^x>`’. In both cases, Calc also writes the nameless function to the Trail so that you can get it back later if you wish.

If there is only one argument, you can write ‘`#`’ in place of ‘`#1`’. (Note that ‘`< >`’ notation is also used for date forms. Calc tells that ‘`<stuff>`’ is a nameless function by the presence of ‘`#`’ signs inside stuff, or by the fact that stuff begins with a list of variables followed by a colon.)

You can type a nameless function directly to `V A '`, or put one on the stack and use it with `V A $`. Calc will not prompt for an argument list in this case, since the nameless function specifies the argument list as well as the function itself. In `V A '`, you can omit the ‘`< >`’ marks if you use ‘`#`’ notation for the arguments, so that `V A ' #1+#2 <RET>` is the same as `V A ' <#1+#2> <RET>`, which in turn is the same as `V A ' $$+$ <RET>`.

The internal format for ‘`<x, y : x + y>`’ is ‘`lambda(x, y, x + y)`’. (The word `lambda` derives from Lisp notation and the theory of functions.) The internal format for ‘`<#1 + #2>`’ is ‘`lambda(ArgA, ArgB, ArgA + ArgB)`’. Note that there is no actual Calc function called `lambda`; the whole point is that the `lambda` expression is used in its symbolic form, not evaluated for an answer until it is applied to specific arguments by a command like `V A` or `V M`.

(Actually, `lambda` does have one special property: Its arguments are never evaluated; for example, putting ‘`<(2/3) #>`’ on the stack will not simplify the ‘`2/3`’ until the nameless function is actually called.)

As usual, commands like `V A` have algebraic function name equivalents. For example, `V A k g` with an argument of ‘`v`’ is equivalent to ‘`apply(gcd, v)`’. The first argument specifies the operator name, and is either a variable whose name is the same as the function name, or a nameless function like ‘`<#^3+1>`’. Operators that are normally written as algebraic symbols have the names `add`, `sub`, `mul`, `div`, `pow`, `neg`, `mod`, and `vconcat`.

The `call` function builds a function call out of several arguments: ‘`call(gcd, x, y)`’ is the same as ‘`apply(gcd, [x, y])`’, which in turn is the same as ‘`gcd(x, y)`’. The first argument of `call`, like the other functions described here, may be either a variable naming a function, or a nameless function (‘`call(<#1+2#2>, x, y)`’ is the same as ‘`x + 2y`’).

(Experts will notice that it's not quite proper to use a variable to name a function, since the name `gcd` corresponds to the Lisp variable `var-gcd` but to the Lisp function `calcFunc-gcd`. Calc automatically makes this translation, so you don't have to worry about it.)