Skip to content

Commit 81b6abe

Browse files
committed
Add tutorial structure. Add the first two sections.
1 parent de88de6 commit 81b6abe

22 files changed

+505
-64
lines changed

‎src/main/scala/scalatutorial/FunctionsAndEvaluationSection.scala‎

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package scalatutorial
2+
3+
import org.scalaexercises.definitions.Library
4+
5+
import sections._
6+
7+
/** Quickly learn Scala through an interactive tutorial.
8+
*
9+
* @param name scala_tutorial
10+
*/
11+
object ScalaTutorial extends Library {
12+
val owner = "scala-exercises"
13+
val repository = "exercises-fpprinciples"
14+
override val color = Some("#224951")
15+
val logoPath = "scala-tutorial"
16+
17+
val sections = List(
18+
TermsAndTypes,
19+
DefinitionsAndEvaluation,
20+
FunctionalLoops,
21+
LexicalScopes,
22+
TailRecursion,
23+
StructuringInformation,
24+
HigherOrderFunctions,
25+
StandardLibrary,
26+
SyntacticConveniences,
27+
ObjectOrientedProgramming,
28+
ImperativeProgramming,
29+
ClassesVsCaseClasses,
30+
LazyEvaluation,
31+
PolymorphicTypes,
32+
TypeClasses
33+
)
34+
}

‎src/main/scala/scalatutorial/ScalaTutorialLibrary.scala‎

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalatutorial.sections
2+
3+
/** @param name classes_vs_case_classes */
4+
object ClassesVsCaseClasses extends ScalaTutorialSection {
5+
6+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package scalatutorial.sections
2+
3+
/** @param name definitions_and_evaluation */
4+
object DefinitionsAndEvaluation extends ScalaTutorialSection {
5+
6+
/**
7+
* = Naming Things =
8+
*
9+
* Consider the following program that computes the area of a disc
10+
* whose radius is `10`:
11+
*
12+
* {{{
13+
* 3.14159 * 10 * 10
14+
* }}}
15+
*
16+
* To make complex expressions more readable we can give meaningful names to
17+
* intermediate expressions:
18+
*
19+
* {{{
20+
* val radius = 10
21+
* val pi = 3.14159
22+
*
23+
* pi * radius * radius
24+
* }}}
25+
*
26+
* Besides making the last expression more readable it also allows us to
27+
* not repeat the actual value of the radius.
28+
*
29+
* = Evaluation =
30+
*
31+
* A name is evaluated by replacing it with the right hand side of its definition
32+
*
33+
* == Example ==
34+
*
35+
* Here are the evaluation steps of the above expression:
36+
*
37+
* {{{
38+
* pi * radius * radius
39+
* 3.14159 * radius * radius
40+
* 3.14159 * 10 * radius
41+
* 31.4159 * radius
42+
* 31.4159 * 10
43+
* 314.159
44+
* }}}
45+
*
46+
* = Methods =
47+
*
48+
* Definitions can have parameters. For instance:
49+
*/
50+
def square(x: Double) = x * x
51+
52+
/**
53+
* And then you can ''call'' a method as follows:
54+
*/
55+
def usingSquare(res0: Double): Unit = {
56+
square(3.0) shouldBe res0
57+
}
58+
59+
/**
60+
* Let’s define a method that computes the area of a disc, given its radius:
61+
*/
62+
def areaExercise(res0: Double): Unit = {
63+
def area(radius: Double): Double = 3.14159 * square(radius)
64+
65+
area(10) shouldBe res0
66+
}
67+
68+
/**
69+
* = Multiple Parameters =
70+
*
71+
* Separate several parameters with commas:
72+
*/
73+
def sumOfSquares(x: Double, y: Double) = square(x) + square(y)
74+
75+
/**
76+
* = Parameters and Return Types =
77+
*
78+
* Function parameters come with their type, which is given after a colon
79+
*
80+
* {{{
81+
* def power(x: Double, y: Int): Double = ...
82+
* }}}
83+
*
84+
* If a return type is given, it follows the parameter list.
85+
*
86+
* = Evaluation of Function Applications =
87+
*
88+
* Applications of parametrized functions are evaluated in a similar way as
89+
* operators:
90+
*
91+
* 1. Evaluate all function arguments, from left to right
92+
* 1. Replace the function application by the function's right-hand side, and, at the same time
93+
* 1. Replace the formal parameters of the function by the actual arguments.
94+
*
95+
* == Example ==
96+
*
97+
* {{{
98+
* sumOfSquares(3, 2+2)
99+
* sumOfSquares(3, 4)
100+
* square(3) + square(4)
101+
* 3 * 3 + square(4)
102+
* 9 + square(4)
103+
* 9 + 4 * 4
104+
* 9 + 16
105+
* 25
106+
* }}}
107+
*
108+
* = The substitution model =
109+
*
110+
* This scheme of expression evaluation is called the ''substitution model''.
111+
*
112+
* The idea underlying this model is that all evaluation does is ''reduce
113+
* an expression to a value''.
114+
*
115+
* It can be applied to all expressions, as long as they have no side effects.
116+
*
117+
* The substitution model is formalized in the λ-calculus, which gives
118+
* a foundation for functional programming.
119+
*
120+
* = Termination =
121+
*
122+
* Does every expression reduce to a value (in a finite number of steps)?
123+
*
124+
* No. Here is a counter-example:
125+
*
126+
* {{{
127+
* def loop: Int = loop
128+
*
129+
* loop
130+
* }}}
131+
*
132+
* = Changing the evaluation strategy =
133+
*
134+
* The interpreter reduces function arguments to values before rewriting the
135+
* function application.
136+
*
137+
* One could alternatively apply the function to unreduced arguments.
138+
*
139+
* For instance:
140+
*
141+
* {{{
142+
* sumOfSquares(3, 2+2)
143+
* square(3) + square(2+2)
144+
* 3 * 3 + square(2+2)
145+
* 9 + square(2+2)
146+
* 9 + (2+2) * (2+2)
147+
* 9 + 4 * (2+2)
148+
* 9 + 4 * 4
149+
* 25
150+
* }}}
151+
*
152+
* = Call-by-name and call-by-value =
153+
*
154+
* The first evaluation strategy is known as ''call-by-value'',
155+
* the second is is known as ''call-by-name''.
156+
*
157+
* Both strategies reduce to the same final values
158+
* as long as
159+
*
160+
* - the reduced expression consists of pure functions, and
161+
* - both evaluations terminate.
162+
*
163+
* Call-by-value has the advantage that it evaluates every function argument
164+
* only once.
165+
*
166+
* Call-by-name has the advantage that a function argument is not evaluated if the
167+
* corresponding parameter is unused in the evaluation of the function body.
168+
*/
169+
def nothing(): Unit = ()
170+
171+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalatutorial.sections
2+
3+
/** @param name functional_loops */
4+
object FunctionalLoops extends ScalaTutorialSection {
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalatutorial.sections
2+
3+
/** @param name higher_order_functions */
4+
object HigherOrderFunctions extends ScalaTutorialSection {
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalatutorial.sections
2+
3+
/** @param name imperative_programming */
4+
object ImperativeProgramming extends ScalaTutorialSection {
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalatutorial.sections
2+
3+
/** @param name lazy_evaluation */
4+
object LazyEvaluation extends ScalaTutorialSection {
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalatutorial.sections
2+
3+
/** @param name lexical_scopes */
4+
object LexicalScopes extends ScalaTutorialSection {
5+
6+
}

0 commit comments

Comments
 (0)