Skip to content

Commit a42d350

Browse files
fungyee-charliesila-strike
authored andcommitted
Update scala-for-java-programmers.md: Rename l to left for clarity
The variable `l` was misleadingly similar to the constant `1` on the website, causing 30 minutes of confusion before realizing it was an alphabet 'L'. Renaming to `left` for the left sub-tree representation enhances readability and eliminates potential ambiguity.
1 parent 7f6db90 commit a42d350

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

_overviews/tutorials/scala-for-java-programmers.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ they can be used to define the type of the trees for our example:
660660
```scala
661661
abstract class Tree
662662
object Tree {
663-
case class Sum(l: Tree, r: Tree) extends Tree
663+
case class Sum(left: Tree, right: Tree) extends Tree
664664
case class Var(n: String) extends Tree
665665
case class Const(v: Int) extends Tree
666666
}
@@ -693,7 +693,7 @@ but also to implement ADTs. Here is how they can be used to define the type
693693
of the trees for our example:
694694
```scala
695695
enum Tree:
696-
case Sum(l: Tree, r: Tree)
696+
case Sum(left: Tree, right: Tree)
697697
case Var(n: String)
698698
case Const(v: Int)
699699
```
@@ -761,7 +761,7 @@ Scala as follows, using a pattern match on a tree value `t`:
761761
import Tree._
762762

763763
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)
765765
case Var(n) => ev(n)
766766
case Const(v) => v
767767
}
@@ -773,7 +773,7 @@ def eval(t: Tree, ev: Environment): Int = t match {
773773
import Tree.*
774774

775775
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)
777777
case Var(n) => ev(n)
778778
case Const(v) => v
779779
```
@@ -784,12 +784,12 @@ def eval(t: Tree, ev: Environment): Int = t match
784784
You can understand the precise meaning of the pattern match as follows:
785785

786786
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
789789
with the evaluation of the expression following the arrow; this
790790
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`,
793793
2. if the first check does not succeed, that is, if the tree is not
794794
a `Sum`, it goes on and checks if `t` is a `Var`; if
795795
it is, it binds the name contained in the `Var` node to a
@@ -852,7 +852,7 @@ obtain the following definition:
852852
import Tree._
853853

854854
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))
856856
case Var(n) if v == n => Const(1)
857857
case _ => Const(0)
858858
}
@@ -864,7 +864,7 @@ def derive(t: Tree, v: String): Tree = t match {
864864
import Tree.*
865865

866866
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))
868868
case Var(n) if v == n => Const(1)
869869
case _ => Const(0)
870870
```

0 commit comments

Comments
 (0)