Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5406c8b
two minor changes
stephen-hero Jul 24, 2021
6c5ed3d
two minor changes
stephen-hero Jul 24, 2021
e97f993
three minor changes
stephen-hero Jul 24, 2021
262fe02
a couple of minor changes
stephen-hero Jul 24, 2021
dbde4c6
a couple of commas
stephen-hero Jul 24, 2021
ed18083
added a couple of words
stephen-hero Jul 25, 2021
cb6ef4b
some rephrasing
stephen-hero Jul 25, 2021
badb8a9
some rewording
stephen-hero Jul 25, 2021
ed1e5c9
a few format revisions
stephen-hero Jul 27, 2021
ea0f658
some punctuation and rephrasing
stephen-hero Jul 27, 2021
b2dbd90
some punctuation and rephrasing
stephen-hero Jul 27, 2021
7188ee8
minor language issues
stephen-hero Jul 27, 2021
030ea11
some rephrasing
stephen-hero Jul 27, 2021
0b119a1
three minor corrections
stephen-hero Jul 27, 2021
416843d
one sentence rephrased
stephen-hero Jul 27, 2021
a32fb0d
one comma
stephen-hero Jul 27, 2021
88a18cd
two errors fixed
stephen-hero Jul 27, 2021
12a260f
some punctuation and wording
stephen-hero Jul 27, 2021
198ff86
some punctuation and rephrasing
stephen-hero Jul 27, 2021
c7f9738
some punctuation and rephrasing; a sentence added in the exercise
stephen-hero Jul 29, 2021
9735276
some grammar, structure, and formatting
stephen-hero Jul 29, 2021
09a68fe
some commas, etc
stephen-hero Jul 29, 2021
6fde2ac
some commas, articles, etc.
stephen-hero Jul 29, 2021
1ef8a17
one preposition
stephen-hero Jul 29, 2021
b4e826d
some grammar and rephrasing
stephen-hero Jul 29, 2021
de7a656
some punctuation
stephen-hero Jul 29, 2021
1723ea2
a few commas, some rephrasing
stephen-hero Jul 29, 2021
50589f5
two commas
stephen-hero Jul 29, 2021
49c0379
a few commas
stephen-hero Jul 29, 2021
9bee241
a few commas
stephen-hero Jul 29, 2021
c53ccd2
slight rephrasing
stephen-hero Jul 30, 2021
498ef76
one comma
stephen-hero Jul 30, 2021
f10ec91
one period
stephen-hero Jul 30, 2021
c801788
some punctuation and rephrasing
stephen-hero Jul 30, 2021
9b1a6d1
punctuation and a typo
stephen-hero Jul 30, 2021
8863a3e
punctuation, extra spaces
stephen-hero Jul 30, 2021
a6ef49a
punctuation
stephen-hero Jul 30, 2021
5f9d91d
formatting
stephen-hero Aug 3, 2021
86c3b03
rephrasing
stephen-hero Aug 3, 2021
b5960e0
word order
stephen-hero Aug 3, 2021
2a3a909
wording, formatting
stephen-hero Aug 3, 2021
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
4 changes: 2 additions & 2 deletions Classes Vs Case Classes/Case Classes Encoding/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ because it is very common in practice.
So, when we define a case class, the Scala compiler defines a class
enhanced with some more methods and a companion object.

For instance, the following case class definition:
For instance, look at the following case class definition:

case class Note(name: String, duration: String, octave: Int)

Expands to the following class definition:
It expands to the following class definition:

class Note(_name: String, _duration: String, _octave: Int) extends Serializable {

Expand Down
6 changes: 3 additions & 3 deletions Classes Vs Case Classes/Creation and Manipulation/task.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

In the previous sections we have seen how case classes could be
used to achieve information aggregation, and also how classes
In the previous sections, we have seen how case classes could be
used to achieve information aggregation and also how classes
could be used to achieve data abstraction or to define stateful
objects.

What are the relationship between classes and case classes? How
What is the relationship between classes and case classes? How
do they differ?

## Creation and Manipulation
Expand Down
6 changes: 3 additions & 3 deletions Classes Vs Case Classes/Equality/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ values, whereas the same definitions of notes lead to equal values.

As we have seen in the previous sections, stateful classes introduce a notion of *identity*
that does not exist in case classes. Indeed, the value of `BankAccount` can change over
time whereas the value of a `Note` is immutable.
time, whereas the value of `Note` is immutable.

In Scala, by default, comparing objects will compare their identity, but in the
case of case class instances, the equality is redefined to compare the values of
Expand All @@ -38,5 +38,5 @@ implement their equality).

## Exercise

- Complete the creation of two instances of the `BankAccount`.
- Complete the creation of two instances of a `c3` `Note`.
- Complete the creation of two instances of `BankAccount`.
- Complete the creation of two instances of `c3` `Note`.
18 changes: 9 additions & 9 deletions Definitions and Evaluation/Multiple Parameters/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ Separate several parameters with commas:

## Parameters and Return Types

Function parameters come with their type, which is given after a colon
Function parameters come with their type, which is given after a colon:

def power(x: Double, y: Int): Double = ...

If a return type is given, it follows the parameter list.

## Val vs Def

The right hand side of a `def` definition is evaluated on each use.
The right-hand side of a `def` definition is evaluated on each use.

The right hand side of a `val` definition is evaluated at the point of the definition
The right-hand side of a `val` definition is evaluated at the point of the definition
itself. Afterwards, the name refers to the value.

val x = 2
Expand All @@ -27,11 +27,11 @@ For instance, `y` above refers to `4`, not `square(2)`.

## Evaluation of Function Applications

Applications of parametrized functions are evaluated in a similar way as
Applications of parametrized functions are evaluated in a way similar to
operators:

1. Evaluate all function arguments, from left to right.
2. Replace the function application by the function's right-hand side, and, at the same time
2. Replace the function application by the function's right-hand side and, at the same time
3. Replace the formal parameters of the function by the actual arguments.

## Example
Expand All @@ -52,7 +52,7 @@ This scheme of expression evaluation is called the *substitution model*.
The idea underlying this model is that all evaluation does is *reduce
an expression to a value*.

It can be applied to all expressions, as long as they have no side effects.
It can be applied to all expressions as long as they have no side effects.

The substitution model is formalized in the λ-calculus, which gives
a foundation for functional programming.
Expand All @@ -69,8 +69,8 @@ No. Here is a counter-example:

## Value Definitions and Termination

The difference between `val` and `def` becomes apparent when the right
hand side does not terminate. Given
The difference between `val` and `def` becomes apparent when the right-hand
side does not terminate. Given

def loop: Int = loop

Expand Down Expand Up @@ -124,5 +124,5 @@ Scala normally uses call-by-value.
## Exercise

Complete the following definition of the `triangleArea` function,
which takes a triangle base and height as parameters and returns
which takes the base and height of a triangle as parameters and returns
its area.
8 changes: 4 additions & 4 deletions Definitions and Evaluation/Naming Things/task.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@

## Naming Things

Consider the following program that computes the area of a disc
Consider the following program, which computes the area of a disc
whose radius is `10`:

3.14159 * 10 * 10

To make complex expressions more readable we can give meaningful names to
To make complex expressions more readable, we can give meaningful names to
intermediate expressions:

val radius = 10
val pi = 3.14159

pi * radius * radius

Besides making the last expression more readable it also allows us to
Besides making the last expression more readable, it also allows us to
not repeat the actual value of the radius.

## Evaluation

A name is evaluated by replacing it with the right hand side of its definition
A name is evaluated by replacing it with the right-hand side of its definition.

### Example

Expand Down
4 changes: 2 additions & 2 deletions Functional Loops/Computing the Square Root of a Value/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

## Computing the Square Root of a Value

We will define in this section a method
In this section, we will define a square root calculation method:

/** Calculates the square root of parameter x */
def sqrt(x: Double): Double = ...
Expand Down Expand Up @@ -38,7 +38,7 @@ Recursive methods need an explicit return type in Scala.

For non-recursive methods, the return type is optional.

Second, we define a method `improve` to improve an estimate and a test to check for termination:
Second, we define a method `improve` to improve the estimate and a test to check for termination:

def improve(guess: Double, x: Double) =
(guess + x / guess) / 2
Expand Down
24 changes: 12 additions & 12 deletions Functional Loops/Conditional Expressions/task.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@

## Conditional Expressions

To express choosing between two alternatives, Scala
has a conditional expression `if-else`.
To express the choice between two alternatives, Scala
uses a conditional expression `if-else`.

It looks like a `if-else` in Java, but is used for expressions, not statements.
It looks like the `if-else` in Java but is used for expressions, not statements.

Example:
~~~
def abs(x: Double) = if (x >= 0) x else -x
~~~
`x >= 0` is a *predicate*, of type `Boolean`.
Here `x >= 0` is a *predicate* of type `Boolean`.

## Boolean Expressions

Boolean expressions `b` can be composed of
Boolean expressions `b` can include:

true false // Constants
!b // Negation
b && b // Conjunction
b || b // Disjunction

and of the usual comparison operations:
e <= e, e >= e, e < e, e > e, e == e, e != e
They can also contain the usual comparison operations:
e <= e, e >= e, e < e, e > e, e == e, e != e.

## Rewrite rules for Booleans

Expand All @@ -41,11 +41,11 @@ We say these expressions use "short-circuit evaluation".

## Summary

You have seen simple elements of functional programing in Scala.
You have seen simple elements of functional programing in Scala:

- arithmetic and boolean expressions
- conditional expressions if-else
- functions with recursion
- arithmetic and boolean expressions;
- conditional expressions if-else;
- functions with recursion.

You have learned the difference between the call-by-name and
call-by-value evaluation strategies.
Expand All @@ -55,4 +55,4 @@ the substitution model.

## Exercise

Complete the following method definition that computes the factorial of a number.
Complete the definition of the following method, which computes the factorial of a number.
20 changes: 10 additions & 10 deletions Higher Order Functions/Higher Order Functions/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ can be passed as a parameter and returned as a result.

This provides a flexible way to compose programs.

Functions that take other functions as parameters or that return functions
as results are called *higher order functions*.
Functions that take other functions as parameters or return functions
as a result are called *higher order functions*.

### Motivation

Consider the following programs.

Take the sum of the integers between `a` and `b`:
Taking the sum of the integers between `a` and `b`:

def sumInts(a: Int, b: Int): Int =
if (a > b) 0 else a + sumInts(a + 1, b)

Take the sum of the cubes of all the integers between `a`
Taking the sum of the cubes of all the integers between `a`
and `b`:

def cube(x: Int): Int = x * x * x

def sumCubes(a: Int, b: Int): Int =
if (a > b) 0 else cube(a) + sumCubes(a + 1, b)

Take the sum of the factorials of all the integers between `a`
Taking the sum of the factorials of all the integers between `a`
and `b`:

def sumFactorials(a: Int, b: Int): Int =
Expand Down Expand Up @@ -72,22 +72,22 @@ Passing functions as parameters leads to the creation of many small functions.

Sometimes it is tedious to have to define (and name) these functions using `def`.

Compare to strings: We do not need to define a string using `val`. Instead of:
Compare to strings: we do not need to define a string using `val`. Instead of:

val str = "abc"; println(str)

We can directly write:
we can directly write:

println("abc")

because strings exist as *literals*. Analogously we would like function
because strings exist as *literals*. Analogously, we can use function
literals, which let us write a function without giving it a name.

These are called *anonymous functions*.

### Anonymous Function Syntax

Example of a function that raises its argument to a cube:
Example of a function that raises its argument to the third power:

(x: Int) => x * x * x

Expand All @@ -108,7 +108,7 @@ can always be expressed using `def` as follows:

{ def f(x1: T1, …, xn: Tn) = e ; f }

where `f` is an arbitrary, fresh name (that's not yet used in the program).
where `f` is an arbitrary, fresh name (that has not yet been used in the program).

One can therefore say that anonymous functions are *syntactic sugar*.

Expand Down
12 changes: 6 additions & 6 deletions Imperative Programming/Imperative Loops/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

In the first sections, we saw how to write loops using recursion.

### While-Loops
### While Loops

We can also write loops with the `while` keyword:

Expand All @@ -14,18 +14,18 @@ We can also write loops with the `while` keyword:
r
}

As long as the condition of a *while* statement is `true`,
As long as the condition of the `while` statement is `true`,
its body is evaluated.

### For-Loops
### For Loops

In Scala there is a kind of `for` loop:
In Scala, there is a kind of `for` loop, too:

for (i <- 1 until 3) { System.out.print(i.toString + " ") }

This displays `1 2`.

For-loops translate similarly to for-expressions, but using the
`For` loops translate similarly to `for` expressions but use the
`foreach` combinator instead of `map` and `flatMap`.

`foreach` is defined on collections with elements of type `A` as follows:
Expand All @@ -37,7 +37,7 @@ Example:

for (i <- 1 until 3; j <- "abc") println(s"$i $j")

translates to:
It translates to:

(1 until 3) foreach (i => "abc" foreach (j => println(s"$i $j")))

Expand Down
Loading