exercises: quote with macros
[jackhill/mal.git] / docs / exercises.md
CommitLineData
9678065e
NB
1# Exercises to learn MAL
2
3The process introduces LISP by describing the internals of selected
4low-level constructs. As a complementary and more traditional
5approach, you may want to solve the following exercises in the MAL
6language itself, using any of the existing implementations.
7
8You are encouraged to use the shortcuts defined in the step files
4f72e010
NB
9(`not`...) and `the `lib/` subdirectory (`reduce`...) whenever you
10find that they increase the readability.
9678065e
NB
11
12The difficulty is progressive in each section, but they focus on
13related topics and it is recommended to start them in parallel.
14
15Some solutions are given in the `examples` directory. Feel free to
16submit new solutions, or new exercises.
17
18## Replace parts of the process with native constructs
19
0544b52f
NB
20Once you have a working implementation, you may want to implement
21parts of the process inside the MAL language itself. This has no other
22purpose than learning the MAL language. Once it exists, a built-in
23implementation will always be more efficient than a native
24implementation. Also, the functions described in MAL process are
25selected for educative purposes, so portability accross
26implementations does not matter much.
27
28You may easily check your answers by passing them directly to the
29interpreter. They will hide the built-in functions carrying the same
30names, and the usual tests (with REGRESS=1) will check them. The
31`runtest.py` script provide a convenient command-line parameter to
32pass a command like 'load-file' before running the testsuite.
9678065e
NB
33```
34make REGRESS=1 TEST_OPTS='--hard --pre-eval=\(load-file\ \"../answer.mal\"\)' test^IMPL^stepA
35```
0544b52f 36
9678065e
NB
37- Implement `nil?`, `true?`, `false?`, `empty?` and `sequential` with
38 another built-in function.
0544b52f
NB
39
40- Implement `>`, `<=` and `>=` with `<`.
41
1ca3ee3d
NB
42- Implement `hash-map`, `list`, `prn` and `swap!` as non-recursive
43 functions.
44
45- Implement `count`, `nth`, `map`, `concat` and `conj` with the empty
46 constructor `()`, `empty?`, `cons`, `first` and `rest`.
47
9678065e
NB
48 You may use `or` to make the definition of `nth` a bit less ugly,
49 but avoid `cond` because its definition refers to `nth`.
50
1ca3ee3d 51 Let `count` and `nth` benefit from tail call optimization.
0544b52f 52
1ca3ee3d 53 Try to replace explicit recursions with calls to `reduce` and `foldr`.
0544b52f 54
42d31a20
NB
55 Once you have tested your solution, you should comment at least
56 `nth`. Many implementations, for example `foldr` in `core.mal`,
57 rely on an efficient `nth` built-in function.
58
0544b52f
NB
59- Implement the `do` special as a non-recursive function. The special
60 form will hide your implementation, so in order to test it, you will
61 need to give it another name and adapt the test accordingly.
62
304930e7
NB
63- Implement `let*` as a macro that uses `fn*` and recursion.
64 The same remark applies.
9678065e
NB
65 A macro is necessary because a function would attempt to evaluate
66 the first argument.
0544b52f 67
7977c2cb
NB
68- Implement quoting with macros.
69 The same remark applies.
70
1ca3ee3d 71- Implement `apply`.
0544b52f
NB
72
73- Implement maps using lists.
304930e7
NB
74 - Recall how maps must be evaluated.
75 - In the tests, you may want to replace `{...}` with `(hash-map ...)`.
76 - An easy solution relies on lists alterning keys and values, so
77 that the `hash-map` is only a list in reverse order so that the
78 last definition takes precedence during searches.
79 - As a more performant solution will use lists to construct trees,
80 and ideally keep them balanced. You will find examples in most
81 teaching material about functional languages.
82 - Recall that `dissoc` is an optional feature. One you can implement
83 dissoc is by assoc'ing a replacement value that is a magic delete
84 keyword (e.g.: `__..DELETED..__`) which allows you to shadow
85 values in the lower levels of the structure. The hash map
86 functions have to detect that and do the right thing. e.g. `(keys
87 ...)` might have to keep track of deleted values as it is scanning
88 the tree and not add those keys when it finds them further down
89 the tree.
0544b52f 90
0544b52f 91- Implement macros within MAL.
9678065e
NB
92
93## More folds
94
95- Compute the sum of a sequence of numbers.
96- Compute the product of a sequence of numbers.
97
98- Compute the logical conjunction ("and") and disjunction ("or") of a
99 sequence of MAL values interpreted as boolean values. For example,
100 `(conjunction [true 1 0 "" "a" nil true {}])`
101 should evaluate to `false` or `nil` because of the `nil` element.
102
103 Why are folds not the best solution here, in terms of average
104 performances?
105
106- Does "-2-3-4" translate to `(reduce - 0 [2 3 4])`?
107
108- Suggest better solutions for
109 `(reduce str "" xs)` and
110 `(reduce concat [] xs)`.
111
112- What does `(reduce (fn* [acc _] acc) xs)` nil answer?
113
114- The answer is `(fn* [xs] (reduce (fn* [_ x] x) nil xs))`.
115 What was the question?
116
117- What is the intent of
118 `(reduce (fn* [acc x] (if (< acc x) x acc)) 0 xs)`?
119
120 Why is it the wrong answer?
121
122- Though `(sum (map count xs))` or `(count (apply concat xs))` can be
123 considered more readable, implement the same effect with a single loop.
124- Compute the maximal length in a list of lists.
125
126- How would you name
127 `(fn* [& fs] (foldr (fn* [f acc] (fn* [x] (f (acc x)))) identity fs))`?