The ElseList object

The ElseList object provide flow of control in a clause that is in some ways similar to an "if/then/else" statement in other programming languages. The difference is that here control is driven by testing for the null object (Code.Null) rather than testing on a Boolean true/false value.

The ElseList object contains a list of two or more clauses (an ElseList with a single clause is allowed, but serves no purpose. Like other monoids, an ElseList with a single parameter is the identity function, see Monoids for details).

Each clause in the ElseList is executed in order. If a clause generates a result other than the null object, that expression is the result of the ElseList and processing stops. If a clause evaluates to null, then processing falls though to the next clause in the list.

If none of the clauses evaluate to a non-null result, then the ElseList evaluates to null.

The ElseList object can be thought of as being derived from the List object through the First function

ElseList[a, b, …] = First[List[a, b, …]]

where First is defined by the clause:

{ f[a, b...] } => a

For performance reasons, ElseList is implemented as a separate built in function. But mathematically its behavior is identical to what we have just described. In the built in function, if one of the clauses in the list evaluates to a result other than null, then processing stops - none of the remaining clauses in the List are evaluated.

Note that the list of clauses in an object definition are evaluated as if they were wrapped in an ElseList object. If the function has the BuildAll property assigned to it, then the list of clauses in the object definition are evaluated as if they were wrapped in a List object.

Here are two examples of clauses that use the ElseList object:

The first example is from the Simplify command

There is also a two parameter Else object that is displayed as:

{ a } => Fn1[a] else Fn2[a]

This object is executed as:

{ a } => ElseList[Fn1[a], Fn2[a]]