Higher-Order Programming » Lab Sessions
Session 1 (Solutions)
Assistant: Steven Keuchel (steven.keuchel@vub.be)
Identifiers and Bindings
Exercise 1
Exercise 2
The following output is generated by the program.
Inside the procedure foo
, the procedure bar
is applied twice. Whenever a procedure is called, a new environment is created where the parameters (given by the parameter list in the procedure), are bound to the argument values given in the application expression (usually, a compound expression = a combination).
This means that bar
sees an environment where n
is not equal to 15
.
Conditionals using if
and cond
Exercise 3
Exercise 4
You can also optimise it by putting the display
before/around the cond
expression...
In order to understand recursion, you first have to understand recursion
Exercise 5
The first solution entails a recursive process, as after (fac (- n 1))
is evaluated the multiplication still needs to be performed.
The second solution entails an iterative process as the recursive call to iter
is in tail-position: thus after the recursive result is evaluated, no other computations need to be performed.
Both solutions are written as recursive programs (programs that make use of themselves), but thanks to tail-call optimisation, only the first describes a recursive process. Recursive processes need a run-time stack to remember pending computations.
Exercise 6
Higher-Order Procedures
Exercise 7
Exercise 8
Bonus Questions
The solutions to the bonus exercises are not published. For feedback, send your solution to the assistent.