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
pp:2023:scala:l03 [2023/03/16 13:24]
andrei.cirpici
pp:2023:scala:l03 [2023/04/02 17:28] (current)
alexandra.udrescu01
Line 1: Line 1:
-====== Lab 2High order functions ​======+====== Lab 3Lists in Scala ======
  
 Objectives: Objectives:
-  * implement and use **higher-order** functions. A **higher-order** function takes other functions ​as parameter or returns them +  * get familiar with **pattern matching** lists, as well as common list operations from Scala and how they work 
-  ​implement ​**curry** and **uncurry** functionsand how they should be properly used (review lecture).+  * get familiar with common ​**higher-order functions** ​over lists (partitionmap, foldRight, foldLeft, filter)
  
-** Create a new Scala worksheet to write your solutions **+==== 3.1. Common list operations ====
  
-===== 2.1 Intro. Functions as parameters ===== +**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.
-**2.1.1** Write a function ​''​apply''​ that takes an integer and return the result ​of the applied ​function ​on the given integerStart from the code stub below:+
 <code scala> <code scala>
-def apply(n: Int, f: Int => Int): Int { +def atLeastk(k: Int, lList[Int]): Boolean ​
-   ​??? +  if (k == 0) ??? 
-}+  else ??? 
 +  ​} 
 +   
 +def atLeastkPred(pred:​ Int => Boolean)(k: Int, l: List[Int]): Boolean = ???
 </​code>​ </​code>​
  
-**2.1.2** Write a function ''​doubler'' ​that returns ​a function ​that doubles the input it receives (an integer)Start from the code stub below:+**3.1.2.** Write a function ​which returns the first ''​n'' ​elements from given list. The function ​should not be implemented as tail-recursive. 
 <code scala> <code scala>
-def doubler(): Int => Int = { +def take(n: Int, l: List[Int]): List[Int= ??? 
-   ??? +//​take(3,​List(1,​2,​3,​4,​5)) = List(1,2,3)
-}+
 </​code>​ </​code>​
  
-===== 2.2 Custom high order functions ===== +**3.1.3.** Write a function which //drops// the first ''​n'' ​elements from a given list. 
-**2.2.1** Define the function ​''​foldWith'' ​which uses an operation ​''​op'' ​to reduce ​range of integers to a value. For instance, ​given that ''​op''​ is addition (+), the result of folding the range 1 to 3 will be 1+2+3=6. ''​foldWith''​ should be curried (it will take the operation and return another function which expects the bounds).+
 <code scala> <code scala>
-def foldWith ​(op(Int,Int) => Int)(start: Int, stop: Int): Int = { +def drop(n: Int, lList[Int]): List[Int??? 
-  def tail_fold(crt: Intacc: Int): Int  ​??? +//drop(3,List(1,2,3,4,5)) = List(4,5)
-  ?? +
-}+
 </​code>​ </​code>​
  
-**2.2.2** Define the function ​''​foldConditional'' ​which extends ''​foldWith''​ by also adding ​a predicate ''​p:​ Int => Int''​''​foldConditional'' ​will reduce only those elements ​of a range which satisfy the predicate.+**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**.
  
 <code scala> <code scala>
-def foldConditional(op: (Int,Int) => Int, p: Int => Boolean)(start: Int, stop: Int): Int = ???+def takeP(p: Int => Boolean)(lList[Int]): List[Int= ??? 
 +//takeP(_%2 == 0)(List(1,​2,​3,​4,​5,​6)) = List(2,4,6)
 </​code>​ </​code>​
  
-**2.2.3** Write a function ​''​foldMap'' ​which takes values $math[a_1, a_2, \ldots, a_k] from range and computes $math[f(a_1)\;​op\;​f(a_2)\;​op\;​\ldots f(a_k)]. +**3.1.5.** Write a function which uses predicate to partition ​(splita list.
-Use the ''​apply''​ and ''​foldWith''​ methods+
 <code scala> <code scala>
-def foldMap(op(Int,Int=> Int, f: Int => Int)(start: Int, stop: Int): Int = ???+def part(p: Int => Boolean)(lList[Int]): (List[Int]List[Int]) = ??? 
 +// part(_%2 == 0)(List(1,​2,​3,​4,​5,​6)) = (List(2,​4,​6),​List(1,​3,​5))
 </​code>​ </​code>​
  
-====2.3 Curry vs Uncurry ===== +==== 3.2. String processing ​==== 
-**2.3.1** Modify ​the function ​below so that it's curry and use it to calculate ''​5*3''​+ 
 +In what follows, we shall encode a String as a list of characters, using the type defined ​below
 <code scala> <code scala>
-def multiply(x:​Int,​ y:Int): Int => x * y+type Str List[Char]
 </​code>​ </​code>​
 +Add this type alias to your code before solving the following exercises.
  
-**2.3.2** Modify the function below so that it's curry and use it to compare 3 numbers and return the maximum+The following is an input testYou can add more examples to it:
 <code scala> <code scala>
-def compare(x: Inty: Intz: Int): Int = +val l: List[Str] = List("​matei@gmail.com"​"​mihai@gmail.com",​ "​tEst@mail.com"​"​email@email.com",​ "​short@ax.ro"​).map(x ​=> x.toList)
-+
-  if x y && ​> z then  +
-    x +
-  else if y > x && y > z then +
-    y +
-  else +
-    z +
-}+
 </​code>​ </​code>​
  
-===== 2.4 Function transformations ===== +Use ''​map''​''​foldr''/''​foldl''​instead ​of recursive functions. 
-The graph of a function can undergo different geometric transformation such as scalingshiftingrotating, mirroring and so on. The result ​of those transformation will also be a function that looks similarly to the originalIn this exercice we will particularly work with linesA line is a linear equation of the form $math[f(x) = a*x + b]+ 
 +**3.2.1.** Remove uppercases from emails. (Do **not** use recursion). Use the Internet to find the appropriate character function.
  
-**2.4.1** Implement a function that shifts a line on Oy axis by a certain amount $math[\Delta y] 
 <code scala> <code scala>
-def shiftOY(lineDouble => Double, delta_y: Double): Double => Double ​+def remUpper(listList[Str]): List[Str] ​= ???
-   ??? +
-}+
 </​code>​ </​code>​
  
-**2.4.2** Implement ​a function ​that shifts ​line on Ox axis by a certain amount $math[\Delta x]+**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). 
 <code scala> <code scala>
-def shiftOY(lineDouble => Doubledelta_xDouble): Double => Double ​+def longer(kIntlistList[Str]): List[Str] ​= ???
-   ??? +
-}+
 </​code>​ </​code>​
  
-**2.4.3** Implement a function that checks if two lines intersect at an integer value from a given interval+**3.2.3.** Count the number of emails longer than k characters. Use ''​foldRight''​. 
 <code scala> <code scala>
-def intersect(line1Double ​=> Doubleline2Double ​=> Double)(start: Int, stop: Int): Boolean ​= {+def howMany(kInt)(list: List[Str]): Int ??? 
 +</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. 
 + 
 +<code scala> 
 +def namesEmails(list:​ List[Str]): List[(StrStr)] = ??? 
 +</​code>​ 
 + 
 +**3.2.5.** Identify the list of the employed domain names (e.g. ''​gmail.com''​). Remove duplicates. Use no recursion. 
 +<code scala> 
 +def domains(lList[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. 
 +<code scala> 
 +def mySplit(lStr): List[Str] = ??? 
 +</​code>​ 
 + 
 +**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> 
 +def domain(list:​ List[Str]): List[Str] = ??? 
 +</​code>​ 
 + 
 + 
 + 
 + 
 +==== 3.3. Gradebooks ==== 
 +More general implementations 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[(Str, Int)] = List((List('​G'​),​3),​ (List('​F'​),​ 10), (List('​M'​),​6),​ (List('​P'​),​4)) 
 +</​code>​  
 + 
 +To make the type signatures more legiblewe can introduce type aliases in Scala: 
 +<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> 
 +def increment(gGradebook): Gradebook = 
 +  g.map(???)  
 +</​code>​ 
 + 
 +**3.3.2.** Find the average grade from a gradebook. You must use ''​foldRight''​. 
 +<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> 
 +def percentage(g:​ Gradebook): (Double,​Double) = ??? 
 +</​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. 
 +<code scala> 
 +def pass(g: Gradebook): List[Str] = ??? 
 +</​code>​ 
 + 
 +**3.3.5.** Implement merge-sort (in ascending order) over gradebooks:​ 
 +<code scala> 
 +def mergeSort(l:​ Gradebook): Gradebook ​= { 
 +   def merge(u: Gradebook, v: Gradebook): Gradebook = ???
    ???    ???
 } }
 +</​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>​ </​code>​