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:2026:scala:l03 [2026/03/09 23:15]
ldaniel
pp:2026:scala:l03 [2026/03/26 07:30] (current)
ldaniel sealed la binaryTree
Line 11: Line 11:
 This definition has already been implemented in Scala, as shown below. Please copy-paste this definition in your worksheet. This definition has already been implemented in Scala, as shown below. Please copy-paste this definition in your worksheet.
 <code scala> <code scala>
-trait IList +sealed ​trait IList 
 case object Void extends IList case object Void extends IList
 case class Cons(x: Int, xs: IList) extends IList case class Cons(x: Int, xs: IList) extends IList
 </​code>​ </​code>​
 +''​! Hint:''​ We use the keyword ''​sealed''​ to specify that all posible implementations of a trait are in the current file (here, Void and Cons for IList).
  
 **3.1.1.** Consider the following axioms for the operator ''​isEmpty''​. **3.1.1.** Consider the following axioms for the operator ''​isEmpty''​.
Line 94: Line 95:
 The binary tree type definition for Scala is given below. Please copy-paste this definition in your Main.scala file from your laboratory project, in order for printTree to work properly. Call print from the function main and click the "​run"​ text above the definition of main to see the result in terminal. The binary tree type definition for Scala is given below. Please copy-paste this definition in your Main.scala file from your laboratory project, in order for printTree to work properly. Call print from the function main and click the "​run"​ text above the definition of main to see the result in terminal.
 <code scala> <code scala>
-trait BinaryTree {+sealed ​trait BinaryTree {
     override def toString: String = super.toString:​ String     override def toString: String = super.toString:​ String
 } }
Line 146: Line 147:
 </​code>​ </​code>​
  
-**3.2.1.** Implement ''​flatten''​ that squashes the tree traversed in preorder into a list:+**3.2.2.** Implement ''​flatten''​ that squashes the tree traversed in preorder into a list:
 <code scala> <code scala>
 def flatten(tree:​ BinaryTree):​ List[Int] = ??? def flatten(tree:​ BinaryTree):​ List[Int] = ???
 </​code>​ </​code>​
  
-**3.2.2.** Define the function ''​tmap''​ which is the Tree a correspondent to map::​(a→b) → [a] → [b]. +**3.2.3.** Define the function ''​tmap''​ which is the Tree a correspondent to map::​(a→b) → [a] → [b]. 
 <code scala> <code scala>
 def tmap(f: Int => Int, tree: BinaryTree) : BinaryTree = ??? def tmap(f: Int => Int, tree: BinaryTree) : BinaryTree = ???
 </​code>​ </​code>​
  
-**3.2.3.** ! Define the function ''​tfoldr'',​ equivalent of foldr for trees: foldr :: (a -> b -> b) -> b -> Tree a -> b+**3.2.4.** ! Define the function ''​tfoldr'',​ equivalent of foldr for trees: foldr :: (a -> b -> b) -> b -> Tree a -> b
 <code scala> <code scala>
 def tfoldr[B](f:​ (Int, B) => B, acc: B, tree: BinaryTree):​ B = ??? def tfoldr[B](f:​ (Int, B) => B, acc: B, tree: BinaryTree):​ B = ???
 </​code>​ </​code>​
  
-**3.2.4.** ! Implement the ''​flattening''​ function using tfoldr. The order of squashing the tree does not necessarily need to match the previous exercise: ​+**3.2.5.** ! Implement the ''​flattening''​ function using tfoldr. The order of squashing the tree does not necessarily need to match the previous exercise: ​
 <code scala> <code scala>
 def flattening(tree:​ BinaryTree):​ List[Int] = ??? def flattening(tree:​ BinaryTree):​ List[Int] = ???