@@ -660,7 +660,7 @@ they can be used to define the type of the trees for our example:
660
660
``` scala
661
661
abstract class Tree
662
662
object Tree {
663
- case class Sum (l : Tree , r : Tree ) extends Tree
663
+ case class Sum (left : Tree , right : Tree ) extends Tree
664
664
case class Var (n : String ) extends Tree
665
665
case class Const (v : Int ) extends Tree
666
666
}
@@ -693,7 +693,7 @@ but also to implement ADTs. Here is how they can be used to define the type
693
693
of the trees for our example:
694
694
``` scala
695
695
enum Tree :
696
- case Sum (l : Tree , r : Tree )
696
+ case Sum (left : Tree , right : Tree )
697
697
case Var (n : String )
698
698
case Const (v : Int )
699
699
```
@@ -761,7 +761,7 @@ Scala as follows, using a pattern match on a tree value `t`:
761
761
import Tree ._
762
762
763
763
def eval (t : Tree , ev : Environment ): Int = t match {
764
- case Sum (l, r ) => eval(l , ev) + eval(r , ev)
764
+ case Sum (left, right ) => eval(left , ev) + eval(right , ev)
765
765
case Var (n) => ev(n)
766
766
case Const (v) => v
767
767
}
@@ -773,7 +773,7 @@ def eval(t: Tree, ev: Environment): Int = t match {
773
773
import Tree .*
774
774
775
775
def eval (t : Tree , ev : Environment ): Int = t match
776
- case Sum (l, r ) => eval(l , ev) + eval(r , ev)
776
+ case Sum (left, right ) => eval(left , ev) + eval(right , ev)
777
777
case Var (n) => ev(n)
778
778
case Const (v) => v
779
779
```
@@ -784,12 +784,12 @@ def eval(t: Tree, ev: Environment): Int = t match
784
784
You can understand the precise meaning of the pattern match as follows:
785
785
786
786
1 . it first checks if the tree ` t ` is a ` Sum ` , and if it
787
- is, it binds the left sub-tree to a new variable called ` l ` and
788
- the right sub-tree to a variable called ` r ` , and then proceeds
787
+ is, it binds the left sub-tree to a new variable called ` left ` and
788
+ the right sub-tree to a variable called ` right ` , and then proceeds
789
789
with the evaluation of the expression following the arrow; this
790
790
expression can (and does) make use of the variables bound by the
791
- pattern appearing on the left of the arrow, i.e., ` l ` and
792
- ` r ` ,
791
+ pattern appearing on the left of the arrow, i.e., ` left ` and
792
+ ` right ` ,
793
793
2 . if the first check does not succeed, that is, if the tree is not
794
794
a ` Sum ` , it goes on and checks if ` t ` is a ` Var ` ; if
795
795
it is, it binds the name contained in the ` Var ` node to a
@@ -852,7 +852,7 @@ obtain the following definition:
852
852
import Tree ._
853
853
854
854
def derive (t : Tree , v : String ): Tree = t match {
855
- case Sum (l, r ) => Sum (derive(l , v), derive(r , v))
855
+ case Sum (left, right ) => Sum (derive(left , v), derive(right , v))
856
856
case Var (n) if v == n => Const (1 )
857
857
case _ => Const (0 )
858
858
}
@@ -864,7 +864,7 @@ def derive(t: Tree, v: String): Tree = t match {
864
864
import Tree .*
865
865
866
866
def derive (t : Tree , v : String ): Tree = t match
867
- case Sum (l, r ) => Sum (derive(l , v), derive(r , v))
867
+ case Sum (left, right ) => Sum (derive(left , v), derive(right , v))
868
868
case Var (n) if v == n => Const (1 )
869
869
case _ => Const (0 )
870
870
```
0 commit comments