PPP Exercises: Iterations


Exercise: Iterations

The type 'IterRep.T' offers a common interface for iteration over bulk types as for example arrays, lists, sets, or bags. It abstracts from the realisation of the iterator for the particular bulk type.
Each bulk type exports a function 'create' that gets an iterator and returns a value of the bulk type, and a function 'elements' that returns an iterator over a given bulk value.
The module 'iter :Iter' summarizes operations on iterators, e.g. 'select', 'join', 'map', 'for each'.
  1. Create a list of the persons appearing in the exercise Exception handling. Convert this list to a set using an iterator.

  2. Write a generic function 'listToSet' that converts a given list of any type to a set using iterators.

  3. Write a function
    select(persons :list.T(Person)  age :Int  p(:String) :Bool)  
           :Iter.T(Person)
    
    that selects all persons from a list 'persons' that are older than 'age' and whose names match a predicate 'p'. Use the module 'iter'.

    Write a function 'iterPrintPerson' that displays an iterator of persons. Combine both functions to print all persons that are older than 18 and that have a name longer than 6 characters.

  4. Implement the following example:

    Mass data:

    Let Person = ...
    Let Persons = ...
    Let Car = ...
    Let Cars = ...
    
    Work with the following values
    tuple 	"Davide"	 25 	end
    tuple 	"Sabina"	 22 	end
    tuple 	"Gilberto"  	 60 	end
    tuple 	"Federica"  	 3 	end
    tuple 	"Michele"  	 26 	end
    
    tuple 	"Corsa" 	120 	"Davide" 	end
    tuple 	"Polo" 		110 	"Michele" 	end
    tuple 	"320i" 		180 	"Michele" 	end
    tuple 	"Civic" 	170 	"Gilberto" 	end
    tuple 	"Spider" 	150 	"Sabina" 	end
    
    Query, parametrized with x of type Int, giving an iteration
    1. Select all cars that are faster than x km/h.
    2. Select all type names of cars that are faster than x km/h.
    3. Select all owner names of cars that are faster than x km/h.
    4. Select all car type names and owner ages of cars that are faster than x km/h.
    Show where the three basic relational operations select, join, and project are realized in the last query.


Ulrike Steffens, 1994