[NEXT] [PREV]    HANDBOOK / GENTLE PRIMER / Describing Data /

Using Terms in Expressions and Patterns

Gentle
Applications
Concepts
Examples
Handbook
Support
Download

When a Gentle program is executed, the actual values of term types are always ground terms. But in the text of a program we also use terms that contain variables as subterms (i.e. as arguments of a functor). In the extreme case the whole term may be just one variable name. A variable serves as a placeholder.

To avoid confusion, variables start with an upper-case letter, and functors start with a lower case letter.

Variables need not be declared. Nevertheless, they are strongly typed. The type is derived by the Gentle compiler, and the use of the variables is checked for consistency.

Depending on the context in which they are used, terms appear in two roles: as expressions and as patterns.

An expression is used to construct a term from given components.

If the variable Hd is of type Color and holds the value red, and if the variable Tl is of type ColorList and holds the value nil, the expression list(Hd, Tl) yields the value list(red, nil).

A value may be decomposed into constituents by matching the value against a pattern.

If the value list(red, nil) is matched against the pattern list(Hd, Tl) the variable Hd is defined as red, and the variable Tl as nil. The matching succeeds.

If the value nil is matched against the pattern list(Hd, Tl), the matching is said to fail.

Matching a pattern against a term means trying to find values for the variables inside the pattern: replacing the variables by these values makes the pattern equal to the term. For example, replacing Hd by red and Tl by nil turns the pattern list(Hd, Tl) into the term list(red, nil).





[NEXT] [PREV]