Skip to content

Commit 7d88efe

Browse files
author
Abhijit Sarkar
committed
Document exercises
1 parent 3d7f461 commit 7d88efe

9 files changed

+56
-0
lines changed

ch02/src/MyList.scala

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ enum MyList[A]:
1010
case Empty()
1111
case Pair(_head: A, _tail: MyList[A])
1212

13+
/*
14+
Exercise: Map
15+
Once you’ve completed iterate, try to implement map in terms of unfold.
16+
*/
1317
def map[B](f: A => B): MyList[B] =
1418
// this match
1519
// case Empty() => Empty()
@@ -50,5 +54,9 @@ object MyList:
5054
def fill[A](n: Int)(elem: => A): MyList[A] =
5155
unfold(0)(_ == n, _ => elem, _ + 1)
5256

57+
/*
58+
Exercise: Iterate
59+
Implement iterate using the same reasoning as we did for fill.
60+
*/
5361
def iterate[A](start: A, len: Int)(f: A => A): MyList[A] =
5462
unfold((0, start))(_._1 == len, _._2, (i, a) => (i + 1, f(a)))

ch02/src/Tree.scala

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ enum Tree[A]:
1010
case Leaf(value: A)
1111
case Node(left: Tree[A], right: Tree[A])
1212

13+
/*
14+
Exercise: Methods for Tree
15+
16+
Write some methods for Tree. Implement
17+
18+
- size, which returns the number of values (Leafs) stored in the Tree;
19+
- contains, which returns true if the Tree contains a given element of type A, and false otherwise; and
20+
- map, which creates a Tree[B] given a function A => B
21+
22+
Use whichever you prefer of pattern matching or dynamic dispatch to implement the methods.
23+
24+
Exercise: Using Fold
25+
Prove to yourself that you can replace structural recursion with calls to fold,
26+
by redefining size, contains, and map for Tree using only fold.
27+
*/
1328
/** @return
1429
* the number of values (Leafs) stored in the Tree
1530
*/
@@ -38,6 +53,10 @@ enum Tree[A]:
3853
// case Tree.Leaf(value) => Tree.Leaf(f(value))
3954
// case Tree.Node(left, right) => Tree.Node(left.map(f), right.map(f))
4055

56+
/*
57+
Exercise: Tree Fold
58+
Implement a fold for Tree defined earlier.
59+
*/
4160
def fold[B](f: (A => B), g: (B, B) => B): B =
4261
this match
4362
case Tree.Node(left, right) => g(left.fold(f, g), right.fold(f, g))

ch03/src/Bool.scala

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def and(l: Bool, r: Bool): Bool =
1414
def `if`[A](t: A)(f: A): A =
1515
l.`if`(r)(False).`if`(t)(f)
1616

17+
/*
18+
Exercise: Or and Not
19+
Test your understanding of Bool by implementing or and not.
20+
*/
1721
def or(l: Bool, r: Bool): Bool =
1822
new Bool:
1923
def `if`[A](t: A)(f: A): A =

ch03/src/Set.scala

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ch03
22

3+
/*
4+
3.6 Exercise: Sets
5+
*/
36
trait Set[A]:
47

58
def contains(elt: A): Boolean

ch03/src/Stream.scala

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ trait Stream[A]:
1616
def head: B = f(self.head)
1717
def tail: Stream[B] = self.tail.map(f)
1818

19+
/*
20+
Exercise: Stream Combinators
21+
Implement filter, zip, and scanLeft on Stream.
22+
*/
1923
def filter(p: A => Boolean): Stream[A] =
2024
lazy val self = if p(head) then this else tail.filter(p)
2125
new Stream[A]:

ch04/src/Display.scala

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ch04
22

3+
/*
4+
4.5 Exercise: Display Library
5+
*/
36
trait Display[A]:
47
def display(value: A): String
58

ch05/src/Expression.scala

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ has every method call in tail position. Due to Scala runtime limitations not all
1616
in tail position can be converted to tail calls, so we reified calls and returns into
1717
data structures used by a recursive loop called a trampoline.
1818
*/
19+
/*
20+
Exercise: Arithmetic
21+
Now it’s your turn to practice using reification. Your task is to implement an interpreter
22+
for arithmetic expressions. An expression is:
23+
24+
- a literal number, which takes a Double and produces an Expression;
25+
- an addition of two expressions;
26+
- a substraction of two expressions;
27+
- a multiplication of two expressions; or
28+
- a division of two expressions;
29+
30+
Reify this description as a type Expression.
31+
*/
1932
enum Expression:
2033
case Literal(value: Double)
2134
case Addition(left: Expression, right: Expression)

ch05/src/ExpressionC.scala

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ch05
22

33
// Continuation-Passing style.
4+
/* Exercise: CPS Arithmetic */
45
enum ExpressionC:
56
case Literal(value: Double)
67
case Addition(left: ExpressionC, right: ExpressionC)

ch05/src/ExpressionT.scala

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ch05
22

3+
/* Exercise: Trampolined Arithmetic */
34
enum ExpressionT:
45
case Literal(value: Double)
56
case Addition(left: ExpressionT, right: ExpressionT)

0 commit comments

Comments
 (0)