Skip to content
Snippets Groups Projects
Commit e7c8dc94 authored by Julien Richard-Foy's avatar Julien Richard-Foy
Browse files

Fixes to progfun1-week4

parent f75eaf04
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: ...@@ -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? When should you use one or the other?
...@@ -72,7 +72,7 @@ Use an algebraic data type to model ...@@ -72,7 +72,7 @@ Use an algebraic data type to model
- a type with a fixed number of alternatives, where - a type with a fixed number of alternatives, where
- all alternatives are pure data types that do not contain methods. - 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 - 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. - Alternatives are complex, consisting of methods as well as parameters.
......
...@@ -79,7 +79,7 @@ Like classes, functions can have type 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. 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: We can then write:
...@@ -139,4 +139,4 @@ Elements are numbered from 0. ...@@ -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. If index is outside the range from `0` up the the length of the list minus one, a `IndexOutOfBoundsException` should be thrown.
\quiz \quiz
\ No newline at end of file
...@@ -120,10 +120,10 @@ To see why, consider the Java code below. ...@@ -120,10 +120,10 @@ To see why, consider the Java code below.
\begin{lstlisting} \begin{lstlisting}
NonEmpty[] a = new NonEmpty[]{ NonEmpty[] a = new NonEmpty[]{
new NonEmpty(1, new Empty(), new Empty())} new NonEmpty(1, new Empty(), new Empty())};
IntSet[] b = a IntSet[] b = a;
b[0] = new Empty() b[0] = new Empty();
NonEmpty s = a[0] NonEmpty s = a[0];
\end{lstlisting} \end{lstlisting}
It looks like we assigned in the last line an `Empty` set to a 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? ...@@ -172,4 +172,4 @@ When you try out this example, what do you observe?
\\ \verb` O ` & A program that compiles and runs without exception \\ \verb` O ` & A program that compiles and runs without exception
\end{tabular} \end{tabular}
-> ->
\quiz \quiz
\ No newline at end of file
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