# Class 10: Continuation Lab

Back to Continuation Basics. On to The Scheme Report.

Held: Monday, February 13, 2006

Summary: Today we ground your understanding of continuations in a laboratory experience.

Related Pages:

Assignments

Notes:

• Don't forget that Math/CS lunch today!
• Office hours end early today.
• There will be time for questions on the homework.

Overview:

• Lab!

## Lab on Continuations

This laboratory was designed by Ogechi Nnadi and was edited by Samuel A. Rebelsky.

Summary: In this lab, you will familiarize yourself with continuations in Scheme.

### Preparation

a. What is the continuation captured in the code below? Predict the value of the expression.

```(+ 1
(let/cc k
(k 5)))
```

b. What is the value the program? If it was different from your prediction, explain why.

c. What do you expect the following expression to return? Why?

```(+ 1
(let/cc k
5))
```

d. Evaluate the expression. If it was different from your prediction, explain why.

e. What do you expect the following expressions to return? Why?

```; Expression 1
(+ 5
(let/cc k1
(* 2
(let/cc k2
3))))
; Expression 2
(+ 5
(let/cc k1
(* 2
(let/cc k2
(k2 3)))))
; Expression 3
(+ 5
(let/cc k1
(* 2
(let/cc k2
(k1 3)))))
```

### Escape Procedures Using Continuations

(The following two exercises are based on chapter13 of Teach Yourself Scheme in Fixnum Days. )

The following is a definition for a program that computes the product of a list of numbers.

```(define list-product
(lambda (s)
(let recur ((s s))
(if (null? s) 1
(* (car s) (recur (cdr s)))))))
```

a. Rewrite `list-product` using continuations so that if a`0` is encountered during the recursive calls, `0` is returned immediately without further calls to `recur`.

Hint! To make sure that the return is immediate, without applying the cached multiplications, see what happens if you return the symbol `zero` when you encounter a `0`.

b. Explain why one might want to to immediately return `0` when a `0` is encountered.

c. Explain how to solve the problem without continuations.

### Generators

Write a procedure `cyc` that takes no arguments and successively generates the "cycles" of the string "abc" in order. For example,

```> (cyc)
"abc"
> (cyc)
"bca"
> (cyc)
"cab"
> (cyc)
"abc"
```

Hint: You can do this with continuations by running a loop that first returns the subsequent cycle, and then stores the rest of the loop (captured as a continuation) in a variable.

Back to Continuation Basics. On to The Scheme Report.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 10 09:02:47 2006.
The source to the document was last modified on Thu Jan 12 09:00:37 2006.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2006S/Outlines/outline.10.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu