Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
pp:2026:scala:l05 [2026/03/25 09:28] ldaniel created |
pp:2026:scala:l05 [2026/05/02 21:34] (current) cosmin.asavoae |
||
|---|---|---|---|
| Line 8: | Line 8: | ||
| Given the following implementation of the natural numbers, solve the next few exercises. | Given the following implementation of the natural numbers, solve the next few exercises. | ||
| <code scala> | <code scala> | ||
| - | trait Nat | + | sealed trait Nat |
| case object Zero extends Nat | case object Zero extends Nat | ||
| case class Succ(x: Nat) extends Nat | case class Succ(x: Nat) extends Nat | ||
| Line 23: | Line 23: | ||
| </code> | </code> | ||
| - | **5.1.3** Write a function which takes an int and converts it to a Nat. | + | **5.1.3** Write a function which takes an Int and converts it to a Nat. Write the inverse function, which takes a Nat and converts it to Int. |
| <code scala> | <code scala> | ||
| def toNat(x: Int): Nat = ??? | def toNat(x: Int): Nat = ??? | ||
| + | def toInt(x: Nat): Int = ??? | ||
| </code> | </code> | ||
| + | |||
| + | <hidden Automatic conversions> | ||
| + | <code scala> | ||
| + | given nat2Int: Conversion[Nat, Int] = toInt | ||
| + | |||
| + | given int2Nat: Conversion[Int, Nat] = toNat | ||
| + | |||
| + | // the second conversion is used to cast params from Int to Nat | ||
| + | // type Nat, by default | ||
| + | val defaultTypeResult = add(5, 7) | ||
| + | // type Int, by type annotation and the first conversion | ||
| + | val intResult: Int = add(5, 7) | ||
| + | </code> | ||
| + | </hidden> | ||
| ==== 5.2 Option ==== | ==== 5.2 Option ==== | ||
| Line 53: | Line 68: | ||
| Given the implementation of binary trees from the previous lab: | Given the implementation of binary trees from the previous lab: | ||
| <code scala> | <code scala> | ||
| - | trait BinaryTree { | + | sealed trait BinaryTree { |
| override def toString: String = super.toString: String | override def toString: String = super.toString: String | ||
| } | } | ||
| Line 81: | Line 96: | ||
| } | } | ||
| - | '\n' + printTree(this) | + | "\n" + printTree(this) |
| } | } | ||
| } | } | ||
| + | def leaf_node(value : Int) : BinaryTree = Node(TVoid, value, TVoid) | ||
| </code> | </code> | ||
| + | |||
| + | 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. | ||
| And arborică: | And arborică: | ||
| <code scala> | <code scala> | ||
| val arborica = Node(Node(leaf_node(-1), 5, TVoid), 1, Node(leaf_node(4), 2, Node(leaf_node(3), 6, leaf_node(7)))) | val arborica = Node(Node(leaf_node(-1), 5, TVoid), 1, Node(leaf_node(4), 2, Node(leaf_node(3), 6, leaf_node(7)))) | ||
| - | arborica.toStringTree | ||
| </code> | </code> | ||
| Line 99: | Line 116: | ||
| </code> | </code> | ||
| - | **5.3.2** Write a function which takes a BinaryTree and returns the number of nodes in its subtree. | + | **5.3.2** Write a function which takes a BinaryTree and returns the total number of nodes in it. |
| <code scala> | <code scala> | ||
| - | def subtree(tree: BinaryTree): Int = ??? | + | def size(tree: BinaryTree): Int = ??? |
| </code> | </code> | ||
| Line 114: | Line 131: | ||
| </code> | </code> | ||
| - | **(!) 5.3.5** Write a function which takes two BinaryTree and tries to assign the second tree as a child of the first. It should return a "container" of a BinaryTree . | + | **(!) 5.3.5** Write a function which takes two BinaryTree and tries to assign the second tree as a child of the first. It should return a "container" of a BinaryTree. The append is successful if the second tree can become a direct child of the first tree's root. |
| <code scala> | <code scala> | ||
| - | def append(tree1: BinaryTree, tree2: BinaryTree): Option[BTree] = ??? | + | def append(tree1: BinaryTree, tree2: BinaryTree): Option[BinaryTree] = ??? |
| </code> | </code> | ||
| Line 122: | Line 139: | ||
| Given the following implementation of expressions, solve the next few exercises. | Given the following implementation of expressions, solve the next few exercises. | ||
| <code scala> | <code scala> | ||
| - | trait Expr | + | sealed trait Expr |
| case class Atom(a: Int) extends Expr | case class Atom(a: Int) extends Expr | ||
| case class Add(e1: Expr, e2: Expr) extends Expr | case class Add(e1: Expr, e2: Expr) extends Expr | ||
| Line 144: | Line 161: | ||
| <hidden> | <hidden> | ||
| - | If you work outside of worksheets, you can define the trait as: | + | <del>If you work outside of worksheets</del>, you can define the trait as: |
| <code scala> | <code scala> | ||
| - | trait Expr { | + | sealed trait Expr { |
| def + (that: Expr): Expr = Add(this, that) | def + (that: Expr): Expr = Add(this, that) | ||
| - | def * (that: Expr): Expr = Mul(this, that) | + | def * (that: Expr): Expr = Mult(this, that) |
| } | } | ||
| case class Atom(a: Int) extends Expr | case class Atom(a: Int) extends Expr | ||
| Line 172: | Line 189: | ||
| will be represented by the list ''[ [1,2,3],[4,5,6],[7,8,9] ]''. | will be represented by the list ''[ [1,2,3],[4,5,6],[7,8,9] ]''. | ||
| + | <hidden Scala definition> | ||
| + | In Scala, we can define this matrix like this: | ||
| + | <code scala> | ||
| + | val mat = List(List(1, 2, 3), List(4, 5, 6), List(7,8,9)) | ||
| + | </code> | ||
| + | Or.. if you like Cons and Nil, like this: | ||
| + | <code scala> | ||
| + | val mat2 = (1::2::3::Nil)::(4::5::6::Nil)::(7::8::9::Nil)::Nil | ||
| + | </code> | ||
| + | </hidden> | ||
| To make signatures more legible, add the //type alias// to your code: | To make signatures more legible, add the //type alias// to your code: | ||
| <code scala> type Matrix = List[List[Int]] </code> | <code scala> type Matrix = List[List[Int]] </code> | ||