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.

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:

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.

Links of Relevance:

U reference manual (in Bulgarian)