Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
pp:2026:scala:l03 [2026/03/09 23:13] ldaniel created |
pp:2026:scala:l03 [2026/03/11 11:28] (current) ldaniel |
||
|---|---|---|---|
| Line 92: | Line 92: | ||
| ===== 3.2 Binary Tree ===== | ===== 3.2 Binary Tree ===== | ||
| - | The type definition for Scala is given below. Please copy-paste this definition in your Main.class from Scala, 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 { | trait BinaryTree { | ||
| Line 146: | Line 146: | ||
| </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] = ??? | ||