Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pp:2024:scala:l04 [2024/03/22 12:53] costin_andrei.vlad created |
pp:2024:scala:l04 [2024/03/25 10:07] (current) costin_andrei.vlad |
||
---|---|---|---|
Line 29: | Line 29: | ||
==== 4.2 Option ==== | ==== 4.2 Option ==== | ||
- | Option = carrier (like a box or a container) for a single or no element, of a given type. (Ex. Some(_) or None) | + | Option = carrier (like a box or a container) for a single or no element, of a given type. (Ex. ''Some(_)'' or ''None'') |
We use Option to write robust functions, in case they return null or fail to return an accepted value. | We use Option to write robust functions, in case they return null or fail to return an accepted value. | ||
- | **(!) 4.2.1** Refactor the function toNat(), so that it takes an integer (a positive or negative number) and returns a "container" of a Nat. | + | ** 4.2.1** Let's revisit the function ''realtrycatch'' now that we have a type that represents the possibility of error. If an error occurs (try function returns ''None''), the catch function will be called instead. |
+ | <code scala> | ||
+ | def realrealtrycatch(t: => Option[Int], c: => Int): Int = { | ||
+ | ??? | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | **(!) 4.2.2** Refactor the function toNat(), so that it takes an integer (a positive or negative number) and returns a "container" of a Nat. | ||
<code scala> | <code scala> | ||
def toNatOpt(x: Int): Option[Nat] = ??? | def toNatOpt(x: Int): Option[Nat] = ??? | ||
</code> | </code> | ||
- | **(!) 4.2.2** Refactor the function add(), so that it takes two "containers" of Nats and returns a "container" of a Nat. | + | **(!) 4.2.3** Refactor the function add(), so that it takes two "containers" of Nats and returns a "container" of a Nat. |
<code scala> | <code scala> | ||
def addOpt(x: Option[Nat], y: Option[Nat]): Option[Nat] = ??? | def addOpt(x: Option[Nat], y: Option[Nat]): Option[Nat] = ??? | ||
Line 95: | Line 102: | ||
</code> | </code> | ||
- | **4.4.1** Write a function which takes an Expression and simplifies it. (Ex. a * (b + c) -> remove parentheses -> ab + ac) | + | **4.4.1** Write a function which takes an Expression and evaluates it. |
+ | <code scala> | ||
+ | def evaluate(e: Expr): Int = ??? | ||
+ | </code> | ||
+ | |||
+ | **4.4.2** Write a function which takes an Expression and simplifies it. (Ex. a * (b + c) -> remove parentheses -> ab + ac) | ||
<code scala> | <code scala> | ||
def simplify(e: Expr): Expr = ??? | def simplify(e: Expr): Expr = ??? | ||
</code> | </code> | ||
- | **4.4.2** Write a function which takes an Expression and evaluates it. | + | **4.4.3** Write a function which takes an Expression and removes 'useless' operations. (Ex. a * 1 -> a, a + 0 -> a) |
<code scala> | <code scala> | ||
- | def evaluate(e: Expr): Int = ??? | + | def optimize(e: Expr): Expr = ??? |
</code> | </code> | ||
+ | <hidden> | ||
+ | If you work outside of worksheets, you can define the trait as: | ||
+ | <code scala> | ||
+ | trait Expr { | ||
+ | def + (that: Expr): Expr = Add(this, that) | ||
+ | def * (that: Expr): Expr = Mul(this, that) | ||
+ | } | ||
+ | case class Atom(a: Int) extends Expr | ||
+ | case class Add(e1: Expr, e2: Expr) extends Expr | ||
+ | case class Mult(e1: Expr, e2: Expr) extends Expr | ||
+ | </code> | ||
+ | With the operators defined, you can create expressions writting: | ||
+ | <code scala> | ||
+ | (Atom(1) + Atom(2) * Atom(3)) + Atom(4) | ||
+ | </code> | ||
+ | instead of: | ||
+ | <code scala> | ||
+ | Add(Add(Atom(1), Mult(Atom(2), Atom(3))), Atom(4)) | ||
+ | </code> | ||
+ | </hidden> | ||
==== 4.5 Matrix manipulation ==== | ==== 4.5 Matrix manipulation ==== | ||
We shall represent matrices as //lists of lists//, i.e. values of type ''[ [Integer ] ]''. Each element in the outer list represents a line of the matrix. | We shall represent matrices as //lists of lists//, i.e. values of type ''[ [Integer ] ]''. Each element in the outer list represents a line of the matrix. |