Differences

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

Link to this comparison view

Next revision
Previous revision
pp:2023:scala:l04 [2023/03/17 15:44]
alexia.ciuclea created
pp:2023:scala:l04 [2023/04/02 17:30] (current)
alexandra.udrescu01
Line 1: Line 1:
-====== Lab 3Lists in Scala ======+====== Lab 4Data 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(xNaturalNumberextends NaturalNumber
-  } +
-   +
-def atLeastkPred(predInt => 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(nIntlList[Int]): List[Int] ​= ??? +def add(xNaturalNumberyNaturalNumber): 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(nIntlList[Int]): List[Int] ​= ??? +def multiply(xNaturalNumberyNaturalNumber): 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 ​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 NaturalNumber.
 <code scala> <code scala>
-def takeP(p: Int => Boolean)(lList[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)(lList[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, leftBTree, rightBTreeextends BTree
 </​code>​ </​code>​
  
-==== 3.2. String processing ==== +**4.2.1** Write function which takes BinaryTree and returns its depth.
- +
-In what follows, we shall encode ​String as 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 testYou 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 lList[Str] = List("​matei@gmail.com",​ "​mihai@gmail.com",​ "​tEst@mail.com",​ "​email@email.com",​ "​short@ax.ro"​).map(x ​=> x.toList)+def evenChildCount(treeBTree): 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(listList[Str]): List[Str] = ???+def flatten(treeBTree): List[Int] = ???
 </​code>​ </​code>​
  
-**3.2.2.** Write a function which removes emails longer than 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 BinaryTree and return the number ​of nodes whose values follow a ceratain rule.
 <code scala> <code scala>
-def longer(kIntlistList[Str]): List[Str] ​= ???+def countNodes(treeBTreecondInt => 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(kInt)(list: List[Str]): Int = ???+def mirror(treeBTree): BTree= ???
 </​code>​ </​code>​
  
-**3.2.4.** Split the list between first names and email domains. What ingredients (auxiliary functions) are necessary? Use either ​fold or a tail-recursive function in your implementation.+==== 4.Matrix manipulation ==== 
 +We shall represent matrices as //lists of lists//, i.evalues of type ''​[ [Integer ] ]''​Each element in the outer list represents ​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 havemost 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 legibleadd 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(listList[Str]): List[Str] ​= ???+def vprod(mMatrix)(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: Matrixm2: 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 IntExample: +**4.3.3** Write a function which adjoins two matrices by adding new rows:
-<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: +$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[(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.+
  
-**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(gGradebook): Gradebook ​= +def vjoin(m1Matrix, m2: Matrix): Matrix ​= ???
-  g.map(???+
 </​code>​ </​code>​
  
-**3.3.2.** Find the average grade from gradebook. You must use ''​foldRight''​. +**4.3.4** Write 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(gGradebook): (Double,​Double) ​= ???+def msum(m1Matrix, 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(gGradebook): List[String] ​= ???+def tr(mMatrix): Matrix ​= ???
 </​code>​ </​code>​
  
-**3.3.5.** Implement merge-sort ​(in ascending orderover 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)
 +  * (Hintwrite 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(lGradebook): Gradebook = { +def mprod(m1Matrixm2Matrix): Matrix ​= ???
-   def merge(u: GradebookvGradebook): 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[String] = ??? 
-</​code>​