This is an old revision of the document!
Lab 06. List applications
In this lab, we will use lists to implement a lot of string processing. The type String is not decomposable as a list, but strings can be converted to lists of Char as follows:
"Some string".toList
For this reason, we will use the following aliases:
type Str = List[Char] type Email = Str
Throughout this lab, use higher-order functions (map, foldRight, foldLeft, filter, zip, partition, etc) whenever possible etc.
==== 6.1. Gradebooks ====
In what follows, we shall encode a gradebook as a list of pairs (<name>,<grade>), where <name> is a String and <grade> is an Int. Example:
<code scala>
type Gradebook = List[(String,Int)] the type Gradebook now refers to a list of pairs of String and Int
val gradebook = List1)
</code> 
Add this type alias to your code before solving the following exercises.
6.1.1. Write a function which adds one point to all students which have a passing grade (>= 5), and leaves all other grades unchanged.
<code scala>
def increment(g: Gradebook): Gradebook =
  g.map(???) 
</code>
6.1.2. Find the average grade from a gradebook. You must use foldRight.
<code scala>
def average(g: Gradebook): Double = ???
</code>
6.1.3. Write a function which takes a gradebook and returns the percentage of failed vs. passed students, as a pair (x,y).
<code scala>
def percentage(g: Gradebook): (Double,Double) = ???
</code>
6.1.4. Write a function which takes a gradebook and returns the list of names which have passed. Use filter and map from Scala.
<code scala>
def pass(g: Gradebook): List[String] = ???
</code>
6.1.5 Write a function which takes a gradebook and reports all passing students in descending order of their grade.
<code scala>
def honorsList(g: Gradebook): List[String] = ???
</code>
6.1.6. We extend the type Gradebook to:
<code scala>
type Name = String
type Lecture = String
type ExtGradebook = List[(Name,Lecture,Int)]
val egradebook = List2)  the first string is the student name, the second is the course, and the final integer is the grade
</code>
Write a function which reports all students that have failed at least one grade (each student will be reported once):
<code scala>
def atLeastOneFail(g: ExtGradebook): List[Name] = ???
</code>
6.1.7.(!) Write a function groupBy, which is a generalisation of the previous exercise:
<code scala>
def groupBy[A,B](l: List[A])(criterion: A ⇒ B): List[(B,List[A])] = ???
</code>
The function groupBy takes a list with elements of type A, a criterion A ⇒ B and produces a list of pairs (c, lp) : (B,List[A]) with the property that each member of the list lp satisfy the criterion with the value c. Suppose f: A ⇒ B is a criterion. Two elements a : A and b : A satisfy the criterion f iff f(a) == f(b). Examples:
<code>
groupBy(List(“john”, “mary”, “bill”))(_.size) = List3), (1, List(“bill”)))
groupBy(List4)(p ⇒ p._1 + p._2) = List5), (6, List6))
</code>
==== 6.2. Lists of emails =====
6.2.1. Write a function which takes a list of emails and extracts the prefix (e.g. example@domain.com becomes example).
<code scala>
def getNames (l: List[Email]): List[Email] = ???
</code>
6.2.2. Write a function which filters out emails belonging to a specific Top-Level-Domain (e.g. those than end in com).
<code scala>
def removeTLD(l: List[Email], tld: Str): List[Email] = ???
</code>
6.2.3. Write a function which checks is there exist identical names under different domains in a list of emails (e.g. ana@amazon.com and ana@gmail.com)
<code scala>
def containsDuplicates(l: List[Email]): List[Email] = ???
</code>
6.2.4. Write a function which reports the number of duplicates of names under different domains in a list of emails. (e.g. [“ana@aol.com”, “ana@aol.ro”, “ana@amazon.com”, “jim@cx.com”, “mary@mail.com” , “mary@mail.ro”] will produce: 2 since there are two duplication instances: for ana and for mary. 
<code scala>
def countDuplicates(l: List[Email]): Int = ???
</code>
6.2.5. Write a function which reports all duplicates of names under different domains in a list of emails. (e.g. [“ana@aol.com”, “ana@aol.ro”, “jim@cx.com”, “mary@mail.com” , “mary@mail.ro”] will produce: "ana@aol.com", "ana@aol.ro"], ["mary@mail.com" , "mary@mail.ro"
<code scala>
def extractDuplicates(l: List[Email]): List[List[Email]] = ???
</code>
6.2.6. Write a function which removes all duplicates** of names under different domains in a list of emails. (e.g. [“ana@aol.com”, “ana@aol.ro”, “jim@cx.com”, “mary@mail.com” , “mary@mail.ro”] will produce: "ana@aol.com", "ana@aol.ro"], ["mary@mail.com" , "mary@mail.ro"
def extractDuplicates(l: List[Email]): List[List[Email]] = ???