Initializing help system before first use

Working with lists


Type: Programming
Rating: 2
Description: A series of examples showing some of Mosel's list handling functionality:
  • definining lists and initializing lists with values (listinit.mos)
  • enumeration and reversal of lists (listenum.mos)
  • operators on lists (listops.mos)
  • enumeration and reversal of lists (listenum.mos)
  • list access functions (merging two ordered lists, listmerge.mos)
File(s): listenum.mos, listinit.mos, listmerge.mos, listops.mos
Data file(s): listinit.dat


listenum.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file listenum.mos 
   ````````````````` 
   Enumerating a list in inverse order.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Oct. 2006
*******************************************************!)

model "Reversing lists"

 declarations
  K,L: list of integer
 end-declarations
 
 L:= [1,2,3,4,5,6,7,8,9,10] 

! Standard enumeration
 forall(i in L) writeln(i)

! Enumeration in inverse order:
 ! 1. Splitting off the last list element
 K:=L
 while (K <> []) writeln(getfirst(splittail(K,1)))

 ! 2. Reversing the list itself
 reverse(L)
 writeln("Reversed: ", L)
 
 ! 3. Reversed copy of the list
 K:=getreverse(L)
 writeln("Reverse once more: ", K, L)

end-model

listinit.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file listinit.mos 
   ````````````````` 
   Initializing lists with values.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2006
*******************************************************!)

model "Initializing lists"

 declarations
  K,L: list of integer
  M,N: array(range,set of integer) of list of string
  S: set of integer
  R: array(range,set of integer) of set of string
 end-declarations
 
 L:= [1,2,3,4,5,6,7,8,9,10]
 M:: (2..4,1)[['A','B','C'],['D','E'],['F','G','H','I']]

 writeln("L: ", L)
 writeln("An entry of M: ", M(2,1))

 initializations from "listinit.dat"
  K  N
 end-initializations

 writeln("K: ", K)
 writeln("An entry of N: ", N(5,3))

 initializations from "listinit.dat"
  S as 'K'  R as 'N'
 end-initializations

 writeln("Reading data into sets:")
 writeln("S: ", S, "\nR: ", R)

end-model

listmerge.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file listmerge.mos 
   `````````````````` 
   Merging two ordered lists into one ordered list.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Aug. 2006
*******************************************************!)

model "Merging lists"

 declarations
  K,L,M: list of integer
 end-declarations
 
 K:= [1,4,5,8,9,10,13]
 L:= [-1,0,4,6,7,8,9,9,11,11] 

 writeln(K,L)
 
 forall(k in K) do
  while (L<>[] and k >= getfirst(L))  M += splithead(L,1)
  M+= [k]
 end-do  
 
 writeln(M)

end-model

listops.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file listops.mos 
   ```````````````` 
   List operations.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2006
*******************************************************!)

model "List operations"

 declarations
  L,M: list of integer
 end-declarations
 
 L:= [1,2,3] + [4,5]; writeln("L (1): ", L) 
 L+= [6,7,8]; writeln("L (2): ", L) 
 L-= [1,2,3]; writeln("L (3): ", L) 

 M:= sum(l in L) [l*2]; writeln("M: ", M)
 
 writeln("L and M are different: ", L<>M)
 
end-model