# § HOP WPO 2 (Solutions)

Assistant: Bjarno Oeyen (bjarno.oeyen@vub.be)

## § General Remarks

• 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 and 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 3: Shared memory in box-and-pointer-diagrams

Before set-car!:

After set-cdr!:

## § Section 2: Higher-Order Procedures on Lists

### § Exercise 4: Sum

sum can also be implemented using the higher-order procedure apply:

The + 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.

## § Section 3: Higher-Order Procedures on Trees

### § 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 Questions

### § Bonus Exercise 1: Cycle

This program displays #0=(8 5 3 . #0#).