# UW-Madison COMPSCI 538 - Lecture 18 Notes (25 pages)

Previewing pages*1, 2, 24, 25*of 25 page document

**View the full content.**## Lecture 18 Notes

Previewing pages *1, 2, 24, 25*
of
actual document.

**View the full content.**View Full Document

## Lecture 18 Notes

0 0 46 views

- Pages:
- 25
- School:
- University of Wisconsin, Madison
- Course:
- Compsci 538 - Introduction to the Theory and Design of Programming Languages

**Unformatted text preview: **

Continuations In our Scheme implementation of list we d like a way to delay doing any multiplies until we know no zeros appear in the list One approach is to build a continuation a function that represents the context in which a function s return value will be used define listC L con cond null L con 1 0 car L 0 else listC cdr L lambda n n con car L CS 538 Spring 2008 182 The top level call is listC L lambda x x For ordinary lists listC expands to a series of multiplies just like list did define id x x listC 1 2 3 id listC 2 3 lambda n n id 1 listC 2 3 lambda n n 1 listC 3 lambda n n 2 1 listC 3 lambda n n 2 listC lambda n n 3 2 listC lambda n n 6 1 6 6 CS 538 Spring 2008 183 But for a list with a zero in it we get a different execution path listC 1 0 3 id listC 0 3 lambda n n id 1 0 No multiplies are done CS 538 Spring 2008 184 Another Example of Continuations Let s redo our list multiply example so that if a zero is seen in the list we return a function that computes the product of all the non zero values and a parameter that is the replacement value for the unwanted zero value The function gives the caller a chance to correct a probable error in the input data We create list2 L Product of all integers in L if no zero appears else lambda n n product of allnonzeros in L CS 538 Spring 2008 185 define list2 L listE L id define listE L con cond null L con 1 0 car L lambda n con n listE cdr L id else listE cdr L lambda m m con car L CS 538 Spring 2008 186 In the following we check to see if list2 returns a number or a function If a function is returned we call it with 1 effectively removing 0 from the list let V list2 L if number V V V 1 CS 538 Spring 2008 187 For ordinary lists list2 expands to a series of multiplies just like list did listE 1 2 3 id listE 2 3 lambda m m id 1 listE 2 3 lambda m m 1 listE 3 lambda m m 2 1 listE 3 lambda m m 2 listE lambda m m 3 2 listE lambda n n 6 1 6 6 CS 538 Spring 2008 188 But for a list with a zero in it we get a different

View Full Document