HOP WPO 2 (Solutions)
Assistant: Bjarno Oeyen (email@example.com)
- Box-and-pointer diagrams: Remember that compound data types are always “by-reference”.
(car (cons 1 2))will evaluate to the number
1, not to a pointer pointing to the first element stored in
(cons 1 2).
- Try to make use of the
let*constructs to avoid calculating the same value multiple times. You would do this in other programming languages as well.
Section 1: Box-and-pointer Diagrams
Exercise 1: Simple box-and-pointer diagram
Exercise 2: Advanced box-and-pointer diagrams
Exercise 3: Shared memory in box-and-pointer-diagrams
Section 2: Higher-Order Procedures on Lists
Exercise 4: Sum
sum can also be implemented using the higher-order procedure
+ procedure takes an arbitrary amount of arguments. E.g. in order to calculate \(a+b+c+d\), you can type
(+ a b c d) instead of
(+ a (+ b (+ c d))).
apply applies the given procedure on a list of arguments.
Exercise 5: Filtering
Section 3: Higher-Order Procedures on Trees
Exercise 6: Sum and depth of a tree
Exercise 7: Mapping the values on a tree.
During the exercise session, the solution to this exercise was given in error. This has been corrected.
Bonus Exercise 1: Cycle
This program displays
#0=(8 5 3 . #0#).