Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:scalalab2 [2022/05/15 23:55] vbadoiu |
pp:scalalab2 [2022/05/19 00:16] (current) mihai.calitescu resolved typos |
||
---|---|---|---|
Line 4: | Line 4: | ||
* get familiar with **pattern matching** lists, as well as common list operations from Scala and how they work | * 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) | * get familiar with common **higher-order functions** over lists (partition, map, foldRight, foldLeft, filter) | ||
+ | * learn about data types in Scala | ||
+ | * Use the knowledge in real world scenarios such as using sockets or iterating through the filesystem | ||
==== I. Common list operations ==== | ==== I. Common list operations ==== | ||
- | **4.1.1.** Write a function which returns true if a list of integers has at least k elements. Use patterns. | + | **1.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 = { |
if (k == 0) ??? | if (k == 0) ??? | ||
else ??? | else ??? | ||
- | } | + | } |
</code> | </code> | ||
- | **4.1.2.** Write a function which returns the first ''n'' elements from a given list. The function should not be implemented as tail-recursive. | + | **1.1.2.** Write a function which returns the first ''n'' elements from a given list. The function should not be implemented as tail-recursive. |
<code scala> | <code scala> | ||
def take(n: Int, l: List[Int]): List[Int] = ??? | def take(n: Int, l: List[Int]): List[Int] = ??? | ||
Line 21: | Line 23: | ||
</code> | </code> | ||
- | **4.1.3.** Write a function which //drops// the first ''n'' elements from a given list. The function should not be implemented as tail-recursive. | ||
- | <code scala> | ||
- | def drop(n: Int, l: List[Int]): List[Int] = ??? | ||
- | //drop(3,List(1,2,3,4,5)) = List(4,5) | ||
- | </code> | ||
- | **4.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**. | + | **1.1.3.** 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 takeP(p: Int => Boolean)(l: List[Int]): List[Int] = ??? | def takeP(p: Int => Boolean)(l: List[Int]): List[Int] = ??? | ||
Line 33: | Line 30: | ||
</code> | </code> | ||
- | **4.1.5.** Write a function which uses a predicate to partition (split) a list. | + | **1.1.4.** Write a function which uses a predicate to partition (split) a list. |
<code scala> | <code scala> | ||
def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? | def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? | ||
Line 39: | Line 36: | ||
</code> | </code> | ||
- | ==== 4.2. Gradebooks ==== | + | ==== 1.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: | 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> | ||
Line 59: | Line 56: | ||
Add this type alias to your code before solving the following exercises. | 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. | + | **1.2.1.** Find the average grade from a gradebook. You must use ''foldRight''. |
- | <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> | <code scala> | ||
def average(g: Gradebook): Double = ??? | def average(g: Gradebook): Double = ??? | ||
</code> | </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). | + | **1.2.2.** 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 percentage(g: Gradebook): (Double,Double) = ??? | ||
</code> | </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. | + | **1.2.3.** Write a function which takes a gradebook and returns the list of names which have passed. Use filter and map from Scala. |
<code scala> | <code scala> | ||
def pass(g: Gradebook): List[String] = ??? | def pass(g: Gradebook): List[String] = ??? | ||
</code> | </code> | ||
- | **4.2.5.** Implement merge-sort (in ascending order) over gradebooks: | + | **1.2.4.** Implement a sorting algorithm such as merge sort (in ascending order) over gradebooks: |
<code scala> | <code scala> | ||
def mergeSort(l: Gradebook): Gradebook = { | def mergeSort(l: Gradebook): Gradebook = { | ||
Line 88: | Line 79: | ||
</code> | </code> | ||
- | **4.2.6** Write a function which takes a gradebook and reports all passing students in **descending** order of their grade. | + | **1.2.5** Using [[https://www.scalatest.org/user_guide/using_assertions|assertions]] check that our merge sort implementation returns the same lists as the [[https://blog.knoldus.com/sorting-in-scala-using-sortedsortby-and-sortwith-function/|stor]] from Scala. |
- | <code scala> | + | |
- | def honorsList(g: Gradebook): List[String] = ??? | + | |
- | </code> | + | |
===== II. Functional data representation ===== | ===== II. Functional data representation ===== | ||
- | ==== 5.1. Nats === | + | ==== 2.1. Nats === |
Consider the following toy implementation of the type ''Nat'' which encodes natural numbers. | Consider the following toy implementation of the type ''Nat'' which encodes natural numbers. | ||
Line 108: | Line 95: | ||
For instance, ''3'' will be encoded as the value: ''Succ(Succ(Succ(Zero)))''. | For instance, ''3'' will be encoded as the value: ''Succ(Succ(Succ(Zero)))''. | ||
- | **5.1.1.** Write a function which implements addition over Nats: | + | **2.1.1.** Write a function which implements addition over Nats: |
<code scala> | <code scala> | ||
def add(n: Nat, m: Nat): Nat = ??? | def add(n: Nat, m: Nat): Nat = ??? | ||
</code> | </code> | ||
- | **5.1.2.** Write a function which converts a ''Nat'' to an ''Int'': | + | **2.1.2.** Write a function which converts a ''Nat'' to an ''Int'': |
<code scala> | <code scala> | ||
def toInt(n: Nat): Int = ??? | def toInt(n: Nat): Int = ??? | ||
</code> | </code> | ||
- | **5.1.3.** Write a function which converts an ''Int'' to a ''Nat''. | + | **2.1.3.** Write a function which converts an ''Int'' to a ''Nat''. |
<code scala> | <code scala> | ||
def fromInt(i: Int): Nat | def fromInt(i: Int): Nat | ||
</code> | </code> | ||
- | ==== 5.2. Binary Search Trees === | + | ==== 2.2. Binary Search Trees === |
In a [[https://en.wikipedia.org/wiki/Binary_search_tree| binary search tree (BST)]], the key of the current node, is always: | In a [[https://en.wikipedia.org/wiki/Binary_search_tree| binary search tree (BST)]], the key of the current node, is always: | ||
Line 136: | Line 123: | ||
</code> | </code> | ||
- | **5.2.1.** Create the tree shown below: | + | **2.2.1.** Create the tree shown below: |
<code scala> | <code scala> | ||
val tree = ??? | val tree = ??? | ||
Line 148: | Line 135: | ||
</code> | </code> | ||
- | **5.2.2.** Implement the method ''size'' which determines the number of non-empty nodes from the BST. | + | **2.2.2.** Implement the method ''size'' which determines the number of non-empty nodes from the BST. |
- | **5.2.3.** Define the method ''contains'', which checks if a given integer is a member of the BST. | + | **2.2.3.** Define the method ''contains'', which checks if a given integer is a member of the BST. |
- | **5.2.4.** Implement the method ''ins'' which inserts a new integer in the BST. **Note:** the insertion must return a new BST (the //binary search tree// property mentioned above must hold after insertion). | + | **2.2.4.** Implement the method ''ins'' which inserts a new integer in the BST. **Note:** the insertion must return a new BST (the //binary search tree// property mentioned above must hold after insertion). |
- | **5.2.5.** Implement a method ''flatten'' which converts a BST into a list of integers. You must carefully choose the flattening method in such a way as to obtain **a sorted list** from the BST. Hint: you may use the list concatenation operator '':::'' (triple colons; example usage: ''List(1,2,3):::List(4,5)''. | + | **2.2.5.** Implement a method ''depth'' which returns the maximal depth of a BST. Hint: use the method: ''_.max(_)''. |
- | **5.2.6.** Implement a method ''depth'' which returns the maximal depth of a BST. Hint: use the method: ''_.max(_)''. | + | **(!) 2.2.6.** Implement a method ''minimum'' which returns the smallest integer from a BST. (If the tree is empty, we return -1). Hint: use the example above, to guide your implementation. |
- | **(!) 5.2.8.** Implement a method ''minimum'' which returns the smallest integer from a BST. (If the tree is empty, we return -1). Hint: use the example above, to guide your implementation. | + | **(!) 5.2.7.** Implement a method ''successor(k)'' which returns **the smallest** integer from the BST, which is **larger** than ''k''. Use the following examples for your implementation: |
- | + | ||
- | **5.2.9.** Implement a similar method ''maximum''. | + | |
- | + | ||
- | **(!) 5.2.10.** Implement a method ''successor(k)'' which returns **the smallest** integer from the BST, which is **larger** than ''k''. Use the following examples for your implementation: | + | |
<code> | <code> | ||
5 t.successor(2) = 5 | 5 t.successor(2) = 5 | ||
Line 171: | Line 154: | ||
</code> | </code> | ||
- | ** (!!) 5.2.11.** Implement a method ''remove(k)'' which removes element ''k'' from the BST. | + | |
+ | ==== III. Scala in practice ==== | ||
+ | |||
+ | **3.1** Write a function reads the contents of a directory and returns True if a given file is in the folder. | ||
+ | |||
+ | **3.2** Look into [[http://jsuereth.com/scala-arm/sockets.html|this example]] and implement a client and echo server using sockets (we are using the Java classes). | ||
+ | |||
+ | **3.3** In the client, send a list to the server. The server will return the maximum number from that list. |