Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fp:lab04 [2022/03/09 20:26]
pdmatei
fp:lab04 [2022/05/15 23:54] (current)
vbadoiu old revision restored (2022/03/25 10:15)
Line 1: Line 1:
 ===== 4. Lists in Scala ===== ===== 4. Lists in Scala =====
 +
 +Objectives:
 +  * get familiar with **pattern matching** lists, as well as common list operations from Scala and how they work
 +  * get familiar with common **higher-order functions** over lists (partition, map, foldRight, foldLeft, filter)
  
 ==== 4.1. Common list operations ==== ==== 4.1. Common list operations ====
  
-**4.1.1.* Write a function which returns true if a list of integers has at least k elements. Use patterns.+**4.1.1.** Write a function which returns true if a list of integers has at least k elements. Use patterns.
 <code scala> <code scala>
 def atLeastk(k: Int, l: List[Int]): Boolean = def atLeastk(k: Int, l: List[Int]): Boolean =
Line 14: Line 18:
 <code scala> <code scala>
 def take(n: Int, l: List[Int]): List[Int] = ??? def take(n: Int, l: List[Int]): List[Int] = ???
-//​take(3,​List(1,​2,​3,​4,​5)) = 3+//​take(3,​List(1,​2,​3,​4,​5)) = List(1,2,3)
 </​code>​ </​code>​
  
Line 35: Line 39:
 </​code>​ </​code>​
  
 +==== 4.2. Gradebooks ====
 +More general implementation of ''​taken'',​ ''​dropn''​ and ''​part''​ are already implemented in Scala and can be used as member functions of lists. Examples are shown below:
 +<code scala>
 +val l = List(1,​2,​3,​4,​5,​6,​7,​8,​9)
 +l.take(3)
 +l.drop(3)
 +l.partition(_%2 == 0)
 +</​code>​
  
 +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>
 +val gradebook = List(("​G",​3),​ ("​F",​ 10), ("​M",​6),​ ("​P",​4))
 +</​code> ​
 +
 +To make the type signatures more legible, we can introduce type aliases in Scala:
 +<code scala>
 +type Gradebook = List[(String,​Int)] //the type Gradebook now refers to a list of pairs of String and Int
 +</​code>​
 +Add this type alias to your code before solving the following exercises.
 +
 +**4.2.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>​
 +
 +**4.2.2.** Find the average grade from a gradebook. You must use ''​foldRight''​.
 +<code scala>
 +def average(g: Gradebook): Double = ???
 +</​code>​
 +
 +**4.2.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>​
 +
 +**4.2.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>​
 +
 +**4.2.5.** Implement merge-sort (in ascending order) over gradebooks:
 +<code scala>
 +def mergeSort(l:​ Gradebook): Gradebook = {
 +   def merge(u: Gradebook, v: Gradebook): Gradebook = ???
 +   ???
 +}
 +</​code>​
 +
 +**4.2.6** 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>​