Skip to content

Update scala-for-java-programmers.md: Rename l to left for clarity #3050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions _overviews/tutorials/scala-for-java-programmers.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ they can be used to define the type of the trees for our example:
```scala
abstract class Tree
object Tree {
case class Sum(l: Tree, r: Tree) extends Tree
case class Sum(left: Tree, right: Tree) extends Tree
case class Var(n: String) extends Tree
case class Const(v: Int) extends Tree
}
Expand Down Expand Up @@ -682,7 +682,7 @@ but also to implement ADTs. Here is how they can be used to define the type
of the trees for our example:
```scala
enum Tree:
case Sum(l: Tree, r: Tree)
case Sum(left: Tree, right: Tree)
case Var(n: String)
case Const(v: Int)
```
Expand Down Expand Up @@ -750,7 +750,7 @@ Scala as follows, using a pattern match on a tree value `t`:
import Tree._

def eval(t: Tree, ev: Environment): Int = t match {
case Sum(l, r) => eval(l, ev) + eval(r, ev)
case Sum(left, right) => eval(left, ev) + eval(right, ev)
case Var(n) => ev(n)
case Const(v) => v
}
Expand All @@ -762,7 +762,7 @@ def eval(t: Tree, ev: Environment): Int = t match {
import Tree.*

def eval(t: Tree, ev: Environment): Int = t match
case Sum(l, r) => eval(l, ev) + eval(r, ev)
case Sum(left, right) => eval(left, ev) + eval(right, ev)
case Var(n) => ev(n)
case Const(v) => v
```
Expand All @@ -773,12 +773,12 @@ def eval(t: Tree, ev: Environment): Int = t match
You can understand the precise meaning of the pattern match as follows:

1. it first checks if the tree `t` is a `Sum`, and if it
is, it binds the left sub-tree to a new variable called `l` and
the right sub-tree to a variable called `r`, and then proceeds
is, it binds the left sub-tree to a new variable called `left` and
the right sub-tree to a variable called `right`, and then proceeds
with the evaluation of the expression following the arrow; this
expression can (and does) make use of the variables bound by the
pattern appearing on the left of the arrow, i.e., `l` and
`r`,
pattern appearing on the left of the arrow, i.e., `left` and
`right`,
2. if the first check does not succeed, that is, if the tree is not
a `Sum`, it goes on and checks if `t` is a `Var`; if
it is, it binds the name contained in the `Var` node to a
Expand Down Expand Up @@ -841,7 +841,7 @@ obtain the following definition:
import Tree._

def derive(t: Tree, v: String): Tree = t match {
case Sum(l, r) => Sum(derive(l, v), derive(r, v))
case Sum(left, right) => Sum(derive(left, v), derive(right, v))
case Var(n) if v == n => Const(1)
case _ => Const(0)
}
Expand All @@ -853,7 +853,7 @@ def derive(t: Tree, v: String): Tree = t match {
import Tree.*

def derive(t: Tree, v: String): Tree = t match
case Sum(l, r) => Sum(derive(l, v), derive(r, v))
case Sum(left, right) => Sum(derive(left, v), derive(right, v))
case Var(n) if v == n => Const(1)
case _ => Const(0)
```
Expand Down
Loading