Exercises in this lecture  previous -- Keyboard shortcut: 'p'        Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 3.8
Capturing a continuation when traversing a list ***


This exercises is strange! Therefore, I discourage you from making it. Most likely, you will be more confused about continuations after having made this exercise than before...

Write a simple recursive function square-list that traverse a list of numbers, with the purpose of squaring each element in the list.

Modify your function such that it captures a continuation of the handling of the third element in the list (if such an element exists). Replace the squared number with this continuation.

Are you able to access the captured contination from the list, and demonstrate how to use it?

Try this:

  > (define xxx (square-list (list 1 2 3 4 5 6)))
  > ((caddr xxx) '()) 

Explain your results (or lack of results). caddr is a convenient composition of car, cdr and cdr.

Now try to assign the captured continuation (with set!) to a global variable remember-continuation. After you have called square-list, play with your captured and stored continuation:

  > (square-list (list 1 2 3 4 5 6))
  > remember-continuation
  > (remember-continuation '())
  > (remember-continuation '(10 11 12))

Discuss and explain the results you obtain.


Solution