Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2023:scala:l04 [2023/03/17 16:08] alexia.ciuclea |
pp:2023:scala:l04 [2023/04/02 17:30] (current) alexandra.udrescu01 |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Lab 3. Lists in Scala ====== | + | ====== Lab 4. Data types in Scala ====== |
Objectives: | Objectives: | ||
- | * get familiar with **pattern matching** lists, as well as common list operations from Scala and how they work | + | * get familiar with **algebraic data types** |
- | * get familiar with common **higher-order functions** over lists (partition, map, foldRight, foldLeft, filter) | + | * get familiar with **pattern matching** and **recursion** with them |
- | ==== 3.1. Common list operations ==== | + | ==== 4.1 Natural Numbers ==== |
- | + | Given the following implementation of the natural numbers, solve the next few exercices. | |
- | **3.1.1.** Write a function which returns true if a list of integers has at least k elements. Use patterns. Write a second function which returns true if the list has at least k elements that satisfy a predicate. | + | |
<code scala> | <code scala> | ||
- | def atLeastk(k: Int, l: List[Int]): Boolean = | + | trait NaturalNumber |
- | if (k == 0) ??? | + | case object Zero extends NaturalNumber |
- | else ??? | + | case class Successor(x: NaturalNumber) extends NaturalNumber |
- | } | + | |
- | + | ||
- | def atLeastkPred(pred: Int => Boolean)(k: Int, l: List[Int]): Boolean = ??? | + | |
</code> | </code> | ||
- | **3.1.2.** Write a function which returns the first ''n'' elements from a given list. The function should not be implemented as tail-recursive. | + | **4.1.1** Write a function which takes two natural numbers, and return their sum. |
<code scala> | <code scala> | ||
- | def take(n: Int, l: List[Int]): List[Int] = ??? | + | def add(x: NaturalNumber, y: NaturalNumber): NaturalNumber = ??? |
- | //take(3,List(1,2,3,4,5)) = List(1,2,3) | + | |
</code> | </code> | ||
- | **3.1.3.** Write a function which //drops// the first ''n'' elements from a given list. The function should not be implemented as tail-recursive. | + | **4.1.2** Write a function which takes two natural numbers, and return their product. |
<code scala> | <code scala> | ||
- | def drop(n: Int, l: List[Int]): List[Int] = ??? | + | def multiply(x: NaturalNumber, y: NaturalNumber): NaturalNumber = ??? |
- | //drop(3,List(1,2,3,4,5)) = List(4,5) | + | |
</code> | </code> | ||
- | **3.1.4.** Write a function which takes a predicate ''p: Int => Boolean'', a list ''l'' and returns a sublist of ''l'' containing those elements for which ''p'' is true. The function should be **curried**. | + | **4.1.3** Write a function which takes an int and converts it to a NaturalNumber. |
<code scala> | <code scala> | ||
- | def takeP(p: Int => Boolean)(l: List[Int]): List[Int] = ??? | + | def toNaturalNumber(x: Int): NaturalNumber = ??? |
- | //takeP(_%2 == 0)(List(1,2,3,4,5,6)) = List(2,4,6) | + | |
</code> | </code> | ||
- | **3.1.5.** Write a function which uses a predicate to partition (split) a list. | + | ==== 4.2 Binary Trees ==== |
+ | Given the following implementation of binary trees, solve the next few exercices. | ||
<code scala> | <code scala> | ||
- | def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? | + | trait BTree |
- | // part(_%2 == 0)(List(1,2,3,4,5,6)) = (List(2,4,6),List(1,3,5)) | + | case object EmptyTree extends BTree |
+ | case class Node(value: Int, left: BTree, right: BTree) extends BTree | ||
</code> | </code> | ||
- | ==== 3.2. String processing ==== | + | **4.2.1** Write a function which takes a BinaryTree and returns its depth. |
- | + | ||
- | In what follows, we shall encode a String as a list of characters, using the type defined below: | + | |
<code scala> | <code scala> | ||
- | type Str = List[Char] | + | def depth(tree: BTree): Int = ??? |
</code> | </code> | ||
- | Add this type alias to your code before solving the following exercises. | ||
- | The following is an input test. You can add more examples to it: | + | **4.2.2** Write a function which takes a BinaryTree and returns the number of nodes with even number of children. |
<code scala> | <code scala> | ||
- | val l: List[Str] = List("matei@gmail.com", "mihai@gmail.com", "tEst@mail.com", "email@email.com", "short@ax.ro").map(x => x.toList) | + | def evenChildCount(tree: BTree): Int = ??? |
</code> | </code> | ||
- | Use ''map'', ''foldr''/''foldl'', instead of recursive functions. | + | **4.2.3** Write a function which takes a BinaryTree and flattens it (turns it into a list containing the values of the nodes). |
- | + | ||
- | **3.2.1.** Remove uppercases from emails. (Do **not** use recursion). Use the Internet to find the appropriate character function. | + | |
<code scala> | <code scala> | ||
- | def remUpper(list: List[Str]): List[Str] = ??? | + | def flatten(tree: BTree): List[Int] = ??? |
</code> | </code> | ||
- | **3.2.2.** Write a function which removes emails longer than a given size. Try to think of two ways to implement this using already defined functions (do not define your own auxiliary functions). | + | **4.2.4** Write a function which takes a BinaryTree and return the number of nodes whose values follow a ceratain rule. |
<code scala> | <code scala> | ||
- | def longer(k: Int, list: List[Str]): List[Str] = ??? | + | def countNodes(tree: BTree, cond: Int => Boolean): Int = ??? |
</code> | </code> | ||
- | **3.2.3.** Count the number of emails longer than k characters. Use ''foldRight''. | + | **4.2.5** Write a function which takes a BinaryTree and return mirrored BTree. |
<code scala> | <code scala> | ||
- | def howMany(k: Int)(list: List[Str]): Int = ??? | + | def mirror(tree: BTree): BTree= ??? |
</code> | </code> | ||
- | **3.2.4.** Split the list between first names and email domains. What ingredients (auxiliary functions) are necessary? Use either a fold or a tail-recursive function in your implementation. | + | ==== 4.3 Matrix manipulation ==== |
+ | We shall represent matrices as //lists of lists//, i.e. values of type ''[ [Integer ] ]''. Each element in the outer list represents a line of the matrix. | ||
+ | Hence, the matrix | ||
- | <code scala> | + | $math[ \displaystyle \left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}\right)] |
- | def namesEmails(list: List[Str]): List[(Str, Str)] = ??? | + | |
- | </code> | + | |
- | **3.2.5.** Identify the list of the employed domain names (e.g. ''gmail.com''). Remove duplicates. Use no recursion. | + | will be represented by the list ''[ [1,2,3],[4,5,6],[7,8,9] ]''. |
- | <code scala> | + | |
- | def domains(l: List[Str]): List[Str] = ??? | + | |
- | </code> | + | |
- | **(!) 3.2.6.** In some previous exercise you have, most likely, used already defined function to split the emails. Try implementing a split function using ''foldRight''. Try to figure out what the accumulator should do. | + | To make signatures more legible, add the //type alias// to your code: |
- | <code scala> | + | <code scala> type Matrix = List[List[Int]] </code> |
- | def mySplit(l: Str): List[Str] = ??? | + | which makes the type-name ''Matrix'' stand for ''[ [Integer] ]''. |
- | </code> | + | |
+ | **4.3.1** Write a function that computes the scalar product with an integer: | ||
+ | |||
+ | $math[ \displaystyle 2 * \left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}\right) = \left(\begin{array}{ccc} 2 & 4 & 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18 \\ \end{array}\right)] | ||
- | **3.2.7.** Generalize the former function for any given character. Use it to implement a function that return the domains without the dot (ex. ''gmail''). | ||
<code scala> | <code scala> | ||
- | def domain(list: List[Str]): List[Str] = ??? | + | def vprod(m: Matrix)(v: Int): Matrix = ??? |
</code> | </code> | ||
+ | **4.3.2** Write a function which adjoins two matrices by extending rows: | ||
+ | $math[ \displaystyle \left(\begin{array}{cc} 1 & 2 \\ 3 & 4\\\end{array}\right) hjoin \left(\begin{array}{cc} 5 & 6 \\ 7 & 8\\\end{array}\right) = \left(\begin{array}{cc} 1 & 2 & 5 & 6 \\ 3 & 4 & 7 & 8\\\end{array}\right) ] | ||
- | |||
- | ==== 3.3. 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> | <code scala> | ||
- | val l = List(1,2,3,4,5,6,7,8,9) | + | def join(m1: Matrix, m2: Matrix): Matrix = ??? |
- | l.take(3) | + | |
- | l.drop(3) | + | |
- | l.partition(_%2 == 0) | + | |
</code> | </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: | + | **4.3.3** Write a function which adjoins two matrices by adding new rows: |
- | <code scala> | + | |
- | val gradebook: List[(Str, Int)] = List((List('G'),3), (List('F'), 10), (List('M'),6), (List('P'),4)) | + | |
- | </code> | + | |
- | To make the type signatures more legible, we can introduce type aliases in Scala: | + | $math[ \displaystyle \left(\begin{array}{cc} 1 & 2 \\ 3 & 4\\\end{array}\right) vjoin \left(\begin{array}{cc} 5 & 6 \\ 7 & 8\\\end{array}\right) = \left(\begin{array}{cc} 1 & 2 \\ 3 & 4 \\ 5 & 6\\ 7 & 8\\ \end{array}\right) ] |
- | <code scala> | + | |
- | type Gradebook = List[(Str,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. | + | |
- | **3.3.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> | <code scala> | ||
- | def increment(g: Gradebook): Gradebook = | + | def vjoin(m1: Matrix, m2: Matrix): Matrix = ??? |
- | g.map(???) | + | |
</code> | </code> | ||
- | **3.3.2.** Find the average grade from a gradebook. You must use ''foldRight''. | + | **4.3.4** Write a function which adds two matrices. |
- | <code scala> | + | |
- | def average(g: Gradebook): Double = ??? | + | |
- | </code> | + | |
- | **3.3.3.** Write a function which takes a gradebook and returns the percentage of failed vs. passed students, as a pair (x,y). | ||
<code scala> | <code scala> | ||
- | def percentage(g: Gradebook): (Double,Double) = ??? | + | def msum(m1: Matrix, m2: Matrix): Matrix = ??? |
</code> | </code> | ||
- | **3.3.4.** Write a function which takes a gradebook and returns the list of names which have passed. Use filter and map from Scala. | + | **4.3.5** Write a function which computes the transposition of a matrix: |
+ | |||
+ | $math[ tr \left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}\right) = \left(\begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \\ \end{array}\right) ] | ||
<code scala> | <code scala> | ||
- | def pass(g: Gradebook): List[Str] = ??? | + | def tr(m: Matrix): Matrix = ??? |
</code> | </code> | ||
- | **3.3.5.** Implement merge-sort (in ascending order) over gradebooks: | + | **4.3.6** Write a function which computes the vectorial product of two matrices. |
+ | * (Hint: start by writing a function which computes $math[a_{ij}] for a given line $math[i] and column $math[j] (both represented as lists)) | ||
+ | * (Hint: write a function which takes a line of matrix m1 and the matrix m2 and computes the respective line from the product) | ||
<code scala> | <code scala> | ||
- | def mergeSort(l: Gradebook): Gradebook = { | + | def mprod(m1: Matrix, m2: Matrix): Matrix = ??? |
- | def merge(u: Gradebook, v: Gradebook): Gradebook = ??? | + | |
- | ??? | + | |
- | } | + | |
</code> | </code> | ||
- | **3.3.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[Str] = ??? | ||
- | </code> |