PLACE |

languages |

RPN in U |

U is a small functional programming language, designed mainly for explorational and educational uses. Both its syntax and its operational rules are very simple and regular. U can be considered a functional-style calculator for several useful types of data, preloaded with a large number of built-in operators and extensible through defining new ones.

Many lanuages have been influential to U, most notably the so called array languages – APL/J/K/Nial – and others of functional flavour, such as FP, Haskell and ML. Nevertheless, in many respects U is significantly different from all of them.

An influence of a special kind is that of P. Henderson's *functional geometry*: a compositional functional style of constructing pictures in the plane. U incorporates an extended version of this compositional minilanguage.

A program in U is a series of definitions and expressions. Expressions are evaluated so that their values are produced (and displayed), and definitions give names to expressions, so that the latter can be referred to within other expressions.

An expression is a row of one or more items. Within the expression, its items are considered binary operators or arguments to such. Each item is either a directly specified value (a primary expression) or a sub-expression in parentheses or braces. Evaluating an expression takes place by evaluating its items in turn, left-to-right, and applying each operator to its arguments as soon as the values of both the operator and the two arguments become available.

The value of a primary or compound expression is one of: a number, a boolean, a string, a sequence, a picture, a function, or undefined (a legal value despite the name). An item in an expression that happens to be an operator must have a function value.

A function has exactly one argument. The latter can be any value. The same holds of a function's result.

The above description of U's expressions implies several points worth being emphasized explicitly.

- An item may have any value, and that value is obtained by evaluating the item. Being a function is an inherent property of a value and the item that represents it, and is unrelated to where that item appears in the program.
- On the other hand, an item is regarded as an operator if and only if it is in an infix position within the expression. In other words, being an operator is a matter of syntax, and ultimately of order: items at even positions and only they are operators. Thus, in
`a b c d e`

…,`b`

,`d`

, … are operators, and`a`

,`c`

,`e`

… arguments;`b`

applies to`a`

and`c`

,`d`

to the result of`a b c`

and`e`

, etc. - Operators are only binary, and there is no precedence between them.
- There is no special notation for calling a function, such as
*f(x)*or similar. To apply a function to an argument, one uses the operator`.`

: for*f*a function and*x*a suitable argument, the expression*f*`.`

*x*(where both*f*and*x*are arguments) computes the result of applying*f*at*x*. - If
*o*is an operator, applying it such as in*x o y*means applying (the function of)*o*to a two-item sequence whose members are (the values of)*x*and*y*. Thus,*x o y*is equivalent to*o*`.[`

*x*`;`

*y*`]`

. - There is no special designation for ‘list of arguments’ to a function. As a function can only take a single argument, there are no others to separate from (although parentheses may be needed to tell how much of the expression is the argument, as e.g. in
`f.(a+b)`

which is different from`f.a+b`

≡`(f.a)+b`

). On the other hand, a sequence value may serve as an argument tuple where needed.

Expressions in braces are special and fall into three kinds. One of them serves to create a context for evaluating an expression, so that the latter makes use of names locally defined within the context. For example, `{a*b ++ a=x-y; b=x+y}`

is equivalent to `(x-y)*(x+y)`

. Conditional expressions evaluate one or more conditions to determine a value that needs to be returned. For example, `?{x<0::-1;x>0::1;0}`

computes the sign of `x`

. Function expressions produce function literals, e.g. `@{x::10*x+3}`

is a function that computes a simple arithmetic transformation of its argument `x`

. Like the simple `{`

…`}`

, conditional and function expressions can have locally defined names.

Apart from strings and pictures, which are special-purpose structures, sequences are the only kind of compound values in U. They are, however, quite useful, directly representing what in other languages exists as dynamic arrays, lists, tuples, trees, or argument sequences. Other types of structures, such as sets and associative arrays, are straightforwardly modelled, and many others are feasible. There are many built-in operators for sequences, most of them also applicable to strings.

U strives to be consistent in its syntax and minimalistic in the number of concepts used. For example:

- Parentheses only delimit subexpressions and string literals, square brackets only enclose sequences, and curly braces – only special expressions: every construct of varying volume is enclosed in pairing characters, and each pair serves a single purpose. (This does not preclude the existence of convenient shortcuts, such as for one-character (e.g.
`'=`

) and alphanumeric (e.g.`"y17`

) strings.) - The syntax for partial application is consistent with the general one for expressions:
*x**o*`_`

and`_`

*o**x*are used for a left and right partial application, respectively. - There is no function definition other than the general value definition of the form
*name*`=`

*expression*. As functions are values, an expression that produces a function can be assigned a name just like any other expression.

As in other functional languages, pattern matching plays an important role in specifying the behaviour of functions within function expressions. In U, patterns are used for decomposing the values of sequences and pictures, and are combined with the use of boolean expressions (guards) for finer control over expression selection for evaluation. Pattern matching is also used in value definitions; e.g. `xs/x=[1;2;5;14]`

≡ `xs=[1;2;5]; x=14`

.

U reference manual (in Bulgarian)

boykobbatgmaildotcom