4.14 Lists

Lists are another important data structure in Oz similarly to Lisp. Therefore, much functionality for lists is provided in the Oz-module List. Again, we only give some examples here and refer to documentation The Oz Base Environment for further information.

Here is an example of a list which might be obtained by reading lexical information on natural language from some file:

%% a list representing a simple lexicon
declare WordReps=[[mary noun nil]
                  [john noun nil]
                  [girl noun determiner]
                  [nice adjective nil]
                  [pretty adjective nil]
                  [the determiner nil]
                  [laughs verb noun]
                  [meets verb [noun noun]]
                  [kisses verb [noun noun]]
                  [embarrasses verb [noun noun]]
                  [thinks verb [verb noun]]
                  [is verb [adjective noun]]
                  [met adjective nil]
                  [kissed adjective nil]
                  [embarrassed adjective nil]]
{Inspect WordReps}

As proposed above, one might wish to represent the features of a word in a more accessible way by using a record rather than a list. For instance, the record word(cat:noun phon:[mary] subcat:nil) is more readable than the list [mary noun nil]. More importantly, it is possible to select a feature of a word in the record representation in constant time, whereas it takes linear time in the number of features in the list representation.

Given the list of list WordReps above, we can compute a list of records Words by converting all representions in WordReps. This can be done by using the functional procedure Map:

%% convert lists to records
declare fun{Convert [P C S]}
           word(phon:[P] cat:C subcat:S)
        end 
declare Words = {Map WordReps Convert}
{Inspect Words}

When Convert is applied it matches its argument against the pattern [P C S] and returns the record word(phon:[P] cat:C subcat:S). More on pattern matching can be found in the next section.

Note that the procedure Map is provided by the module List. Indeed, Map is identical to List.map, as shown when feeding:

{Inspect Map==List.map}

Here, we apply the predefined functional procedure == which compares two Oz-values for equality and returns the Boolean value.

Next, we might want to filter all verbs out of the lexicon Words. This can be done by using the procedure Filter also defined in the module List:

declare Verbs = {Filter Words fun{$ W}
                                 W.cat == verb
                              end}
{Inspect Verbs}

Or else, you might want to test, whether all words in the lexicon are verbs. This can be done by using the function FoldL (or alternatively FoldR).

declare B1 = {FoldL Words fun{$ B W}
                             {And B W.cat==verb}
                          end 
              true}
declare B2 = {FoldR Words fun{$ W B}
                             {And B W.cat==verb}
                          end 
              true}
{Inspect [B1 B2]}

The value of the term {FoldL [X1 ... Xn] P Z} is the same as {P ... {P {P Z X1} X2} ... Xn} i. e. where grouping goes from left to right (hence the L in FoldL). Correspondingly, {FoldR [X1 ... Xn] P Z} evaluates like {P X1 {P X2 ... {P Xn Z} ...}} where grouping goes from right to left (hence the R in FoldR).


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)