Basic lists, as the name suggests, contain items of a basic type which will be one of string, integer, double, decimal, currency or boolean. There are also term lists and axiom lists which share the same characteristics as basic lists but have extra features that are described in the next topic.
The following aspects of Lists are to be covered:
- Declaration
- Export
- Access
- Navigation
List Declaration
A List declaration has following format:
list<type> name
This format is just a minimal list declaration with various features that can be added according to how the list is to be used. A list can be declared in any scope or template. A list declaration, except for one located in a template body, can include an assignment to an item set , The item set consists of a comma-delimited list of literal values enclosed in braces. Here is an example of assigning a set of numbers to an integer list:
List Variable
When a list is declared in the body of a template, then it is referred to as a "list variable" and like other variables it has the potential to be set by unification and/or be passed to the solution. Unlike other variables, a list variable being empty is a valid state as it just means there are no items in the list. A list variable can have one of 2 roles - as either providing access to an external list or as a local list of the template. In the first case it is set by unification and in the latter, it is set by appending items to it.
personalities.taq shows a list variable used to reference an external list. It looks up a database containing person details including Zodiac sign and associated personality traits. The search for "John" returns:
Traits: gentle, affectionate, curious
The Gemini traits are stored as a string list:
John's data contains a "traits" term with a list reference to above list. Other names with various Zodiac signs to try are are Alex, Sue, and Sam,
{"John", "m", 23, "Gemini", list gemini_traits}
...
The "name_match" template's 4th term is a list variable with name "traits" to match on the term with the same name in "persons" axiom list.
(
age,
starsign,
list<string> traits
Appender
Lists can be populated by appending items to the end of the list, which is concatenation. The "+=" operator is used to perform dynamic list concatenation, and a variable which grows a list with this operator is called an "appender". sale-items.taq demonstrates concatenation used to create a list of items for sale. Each item contains an amount in US dollars. A "sale_format" template uses an appender named "memorabilia" to create the query solution:
{ "cap", 15.00 }
{ "t-shirt", 25.89 }
template sale_format
{
currency.USD price
(
memorabilia += text
query format_stock(stock:sale_format)