Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
pp:2024:scala:l04 [2024/03/24 21:35] tpruteanu [4.4 Expression evaluation] |
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 121: | Line 128: | ||
| case class Mult(e1: Expr, e2: Expr) extends Expr | case class Mult(e1: Expr, e2: Expr) extends Expr | ||
| </code> | </code> | ||
| - | With the operators defined, you can create expressions like: | + | With the operators defined, you can create expressions writting: |
| <code scala> | <code scala> | ||
| (Atom(1) + Atom(2) * Atom(3)) + Atom(4) | (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> | </code> | ||
| </hidden> | </hidden> | ||