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:2025:scala:l03 [2025/03/16 00:32]
cata_chiru
pp:2025:scala:l03 [2025/03/28 12:04] (current)
cata_chiru
Line 103: Line 103:
  
 case class Node(left: BinaryTree, info: Int, right: BinaryTree) extends BinaryTree { case class Node(left: BinaryTree, info: Int, right: BinaryTree) extends BinaryTree {
-    ​override def toString: String = { +  ​override def toString: String = { 
-        def prettyPrint(tree: BinaryTree, ​depthInt): String = { +    def printTree( 
-            tree match { +                   tree: BinaryTree, 
-                case TVoid =>"\t" * (depth + 1) +  tree.toString() + "\n+                   ​prefixString = "",​ 
-                case Node(l, value, r) => +                   ​isLeft:​ Boolean = true 
-                    "\t" ​* (depth ​1) + value.toString() + "​\n" ​+ +                 ): String = 
-                    ​prettyPrint(l, depth+1) + prettyPrint(r,​ depth+1) +      tree match { 
-            } +        case TVoid => 
-        ​}+          ​""​ 
 +        case Node(l, value, r) => 
 +          val rightStr = 
 +            printTree(r,​ prefix + (if (isLeft && prefix.nonEmpty) ​"│   " ​else " ​   "), isLeft = false) 
 +          val nodeStr = 
 +            prefix ​(if (tree != this) if (isLeft) "​└── " else "​┌── " else " ​   ") + value.toString + "​\n"​ 
 +          val leftStr = 
 +            printTree(l, prefix ​(if (isLeft" ​   " else "​│ ​  "​)) 
 +          rightStr ​nodeStr ​leftStr 
 +      }
  
-        prettyPrint(this, 0+    '​\n'​ + printTree(this) 
-    }+  }
 } }
 </​code>​ </​code>​
Line 138: Line 147:
  
 **3.2.1.** Implement ''​flatten''​ that squashes the tree traversed in preorder into a list: **3.2.1.** Implement ''​flatten''​ that squashes the tree traversed in preorder into a list:
 +<code scala>
 def flatten(tree:​ BinaryTree):​ List[Int] = ??? def flatten(tree:​ BinaryTree):​ List[Int] = ???
 +</​code>​
  
--- 7. Define ​list concatenation over type List a:  +**3.2.2.** ​Define ​the function ''​tmap''​ which is the Tree correspondent to map::(a→b→ [a] → [b].  
-app :: (List a) -> (List a) -(List a) +<code scala> 
-app GVoid GVoid GVoid +def tmap(f: Int =Int, tree: BinaryTree: BinaryTree ​??? 
-app l1 GVoid = l1 +</​code>​
-app GVoid l2 = l2 +
-app (GenCel val pointer) l2 = GenCel val (app pointer l2)+
  
--- 8. Define the function ​tmap which is the Tree a correspondent to map::(a→b) → [a] → [b].  +**3.2.3.** ! Define the function ​''​tfoldr'',​ equivalent of foldr for treesfoldr :: (a -> b -> b) -> b -> Tree a -> b 
-tmap:: (a->b) -> (Tree a-> (Tree b) +<code scala> 
-tmap f TVoid = TVoid +def tfoldr[B](f(Int, B) => B, acc: B, tree: BinaryTree): B = ??? 
-tmap f (Node l k r) = Node (tmap f l) (f k) (tmap f r)+</​code>​
  
--- 10Define ​the function tfoldr:  +**3.2.4.** ! Implement ​the ''​flattening'' ​function ​using tfoldr. The order of squashing the tree does not necessarily need to match the previous exercise:  
-tfoldr :: (a -> b -> b) -> b -> Tree a -b +<code scala
-tfoldr f acc TVoid = acc +def flattening(tree: BinaryTree): List[Int] ​??? 
-tfoldr f acc (Node l k r) = tfoldr f (tfoldr f (f k acc) l) r+</​code>​
  
--- 11. Implement the flattening function using tfoldr: ​ 
-flattening :: Tree a -> List a 
-flattening (Node l k r) = tfoldr GenCel GVoid (Node l k r)