Skip to content
Snippets Groups Projects
Commit 1d9306c9 authored by Martin Odersky's avatar Martin Odersky
Browse files

Merge branch 'changes-to-progfun1-week4' into 'master'

Fixes to progfun1-week4

See merge request lamp/cs-210-functional-programming-2019!9
parents 90ce62f3 e7c8dc94
No related branches found
No related tags found
No related merge requests found
......@@ -60,10 +60,10 @@ For instance, the `Student` type expands to something like:
}
~~~
Relationship With Case Classes(2)
================================
Comparison With Object Oriented Decomposition
=============================================
Case classes and algebraic data types are very similar.
OO decomposition and algebraic data types are two ways of defining types and operations.
When should you use one or the other?
......@@ -72,7 +72,7 @@ Use an algebraic data type to model
- a type with a fixed number of alternatives, where
- all alternatives are pure data types that do not contain methods.
Use a hierarchy with classes or case classes, if
Use a hierarchy with classes, if
- The set of alternatives is open, i.e. new alternatives can be added after the fact, or
- Alternatives are complex, consisting of methods as well as parameters.
......
......@@ -79,7 +79,7 @@ Like classes, functions can have type parameters.
For instance, here is a function that creates a list consisting of a single element.
def singleton[T](elem: T) = Cons[T](elem, Nil[T]())
def singleton[T](elem: T) = Node[T](elem, Empty[T]())
We can then write:
......@@ -139,4 +139,4 @@ Elements are numbered from 0.
If index is outside the range from `0` up the the length of the list minus one, a `IndexOutOfBoundsException` should be thrown.
\quiz
\ No newline at end of file
\quiz
......@@ -120,10 +120,10 @@ To see why, consider the Java code below.
\begin{lstlisting}
NonEmpty[] a = new NonEmpty[]{
new NonEmpty(1, new Empty(), new Empty())}
IntSet[] b = a
b[0] = new Empty()
NonEmpty s = a[0]
new NonEmpty(1, new Empty(), new Empty())};
IntSet[] b = a;
b[0] = new Empty();
NonEmpty s = a[0];
\end{lstlisting}
It looks like we assigned in the last line an `Empty` set to a
......@@ -172,4 +172,4 @@ When you try out this example, what do you observe?
\\ \verb` O ` & A program that compiles and runs without exception
\end{tabular}
->
\quiz
\ No newline at end of file
\quiz
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment