2.7 Lists

Scilab has a list data type. The list is a collection of data objects not necessarily of the same type. A list can contain any of the already discussed data types (including functions) as well as other lists. Lists are useful for defining structured data objects.

There are two kinds of lists, ordinary lists and typed-lists. A list is defined by the list function. Here is a simple example:

-->L=list(1,'w',ones(2,2))  //L is a list made of 3 entries
 L  =
 
 
       L(1)
 
    1.  
 
       L(2)
 
 w   
 
       L(3)
 
!   1.    1. !
!   1.    1. !

-->L(3)   //extracting entry 3 of list L
 ans  =
 
!   1.    1. !
!   1.    1. !

-->L(3)(2,2)  //entry 2,2 of matrix L(3)
 ans  =
 
    1.  

-->L(2)=list('w',rand(2,2)) //nested list: L(2) is now a list
 L  =
 
 
       L(1)
 
    1.  
 
       L(2)
 
 
        L(2)(1)
 
 w   
 
        L(2)(2)
 
!   0.6653811    0.8497452 !
!   0.6283918    0.6857310 !
 
       L(3)
 
!   1.    1. !
!   1.    1. !

-->L(2)(2)(1,2)  //extracting entry 1,2 of entry 2 of L(2)
 ans  =
 
    0.8497452  

-->L(2)(2)(1,2)=5; //assigning a new value to this entry.

Typed lists have a specific first entry. This first entry must be a character string (the type) or a vector of character string (the first component is then the type, and the following elements the names given to the entries of the list). Typed lists entries can be manipulated by using character strings (the names) as shown below.

-->L=tlist(['Car';'Name';'Dimensions'],'Nevada',[2,3])
 L  =
 
 
       L(1)
 
!Car         !
!            !
!Name        !
!            !
!Dimensions  !
 
       L(2)
 
 Nevada   
 
       L(3)
 
!   2.    3. !

-->L.Name    //same as L(2)
 ans  =
 
 Nevada   
 
-->L.Dimensions(1,2)=2.3

 L  =
 
 
       L(1)
 
!Car         !
!            !
!Name        !
!            !
!Dimensions  !
 
       L(2)
 
 Nevada   
 
       L(3)
 
!   2.    2.3 !
 

-->L(3)(1,2)
 ans  =
 
    2.3  

-->L(1)(1)
 ans  =
 
 Car
An important feature of typed-lists is that it is possible to define operators acting on them (overloading), i.e., it is possible to define e.g. the multiplication L1*L2 of the two typed lists L1 and L2. An example of use is given below, where linear systems manipulations (concatenation, addition, multiplication,...) are done by such operations.