@@ -649,7 +649,7 @@ they can be used to define the type of the trees for our example:
649
649
``` scala
650
650
abstract class Tree
651
651
object Tree {
652
- case class Sum (l : Tree , r : Tree ) extends Tree
652
+ case class Sum (left : Tree , right : Tree ) extends Tree
653
653
case class Var (n : String ) extends Tree
654
654
case class Const (v : Int ) extends Tree
655
655
}
@@ -682,7 +682,7 @@ but also to implement ADTs. Here is how they can be used to define the type
682
682
of the trees for our example:
683
683
``` scala
684
684
enum Tree :
685
- case Sum (l : Tree , r : Tree )
685
+ case Sum (left : Tree , right : Tree )
686
686
case Var (n : String )
687
687
case Const (v : Int )
688
688
```
@@ -750,7 +750,7 @@ Scala as follows, using a pattern match on a tree value `t`:
750
750
import Tree ._
751
751
752
752
def eval (t : Tree , ev : Environment ): Int = t match {
753
- case Sum (l, r ) => eval(l , ev) + eval(r , ev)
753
+ case Sum (left, right ) => eval(left , ev) + eval(right , ev)
754
754
case Var (n) => ev(n)
755
755
case Const (v) => v
756
756
}
@@ -762,7 +762,7 @@ def eval(t: Tree, ev: Environment): Int = t match {
762
762
import Tree .*
763
763
764
764
def eval (t : Tree , ev : Environment ): Int = t match
765
- case Sum (l, r ) => eval(l , ev) + eval(r , ev)
765
+ case Sum (left, right ) => eval(left , ev) + eval(right , ev)
766
766
case Var (n) => ev(n)
767
767
case Const (v) => v
768
768
```
@@ -773,12 +773,12 @@ def eval(t: Tree, ev: Environment): Int = t match
773
773
You can understand the precise meaning of the pattern match as follows:
774
774
775
775
1 . it first checks if the tree ` t ` is a ` Sum ` , and if it
776
- is, it binds the left sub-tree to a new variable called ` l ` and
777
- the right sub-tree to a variable called ` r ` , and then proceeds
776
+ is, it binds the left sub-tree to a new variable called ` left ` and
777
+ the right sub-tree to a variable called ` right ` , and then proceeds
778
778
with the evaluation of the expression following the arrow; this
779
779
expression can (and does) make use of the variables bound by the
780
- pattern appearing on the left of the arrow, i.e., ` l ` and
781
- ` r ` ,
780
+ pattern appearing on the left of the arrow, i.e., ` left ` and
781
+ ` right ` ,
782
782
2 . if the first check does not succeed, that is, if the tree is not
783
783
a ` Sum ` , it goes on and checks if ` t ` is a ` Var ` ; if
784
784
it is, it binds the name contained in the ` Var ` node to a
@@ -841,7 +841,7 @@ obtain the following definition:
841
841
import Tree ._
842
842
843
843
def derive (t : Tree , v : String ): Tree = t match {
844
- case Sum (l, r ) => Sum (derive(l , v), derive(r , v))
844
+ case Sum (left, right ) => Sum (derive(left , v), derive(right , v))
845
845
case Var (n) if v == n => Const (1 )
846
846
case _ => Const (0 )
847
847
}
@@ -853,7 +853,7 @@ def derive(t: Tree, v: String): Tree = t match {
853
853
import Tree .*
854
854
855
855
def derive (t : Tree , v : String ): Tree = t match
856
- case Sum (l, r ) => Sum (derive(l , v), derive(r , v))
856
+ case Sum (left, right ) => Sum (derive(left , v), derive(right , v))
857
857
case Var (n) if v == n => Const (1 )
858
858
case _ => Const (0 )
859
859
```
0 commit comments