DOC PREVIEW
UW CSE 341 - Lecture Notes

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 341:Programming LanguagesDan GrossmanFall 2004Lecture 16— define-struct, let/cc for exceptionsDan Grossman CSE341 Fall 2004, Lecture 16 1'&$%Data in SchemeRecall ML’s approach to each-of, one-of, and self-referential types.Pure Scheme’s approach:• There is One Big Datatype with built-in predicates.• Use pairs (lists) for each-of types.• Primitives implicitly raise errors for “wrong variant”• Use helper functions like caddr and your own.We’ll discuss advantages/disadvantages next week.Dan Grossman CSE341 Fall 2004, Lecture 16 2'&$%define-structMzScheme extends Scheme with define-struct, e.g.:(define-struct square (x y))(define-struct piece (squares))Semantics:• Binds constructors (make-square, make-piece) that takearguments and make values.• Binds predicates (square?, piece?) that take one argument andreturn #t only for values built from the right constructor.• Binds accessors (square-x, square-y, piece-squares) thattake one argument, return the appropriate field, and call error forvalues not built from the right constructor.• Binds mutators (set-square-x!, set-square-y!,set-piece-squares!).Dan Grossman CSE341 Fall 2004, Lecture 16 3'&$%define-struct is speci aldefine-struct creates a new variant for t he One Big Datatype.Claim: define-struct is not a function.Claim: define-struct is not a macro.It could be a macro except for one key bit of its semantics: Valuesbuilt from the constructor cause every other predicate (including allbuilt-in ones) to return #f.Advantage: abstractionDisadvantage: Can’t write “generic” code that has a case for everypossible variant in every Scheme program.Dan Grossman CSE341 Fall 2004, Lecture 16 4'&$%Idiom for ML datatypesInstead of a datatype with n constructors, you just usedefine-struct n times.That “these n go together” is just convention.Instead of case, you have a cond with n predicates and one“catch-all” error case.Dan Grossman CSE341 Fall 2004, Lecture 16 5'&$%Exceptions in SchemeRecall exceptions in Java, ML: Transfer control to nearest dynamicallyscoped exception handler (i.e., nearest on “call stack”).Transfer control: Forget what you’re doing. Result of entire program isnow result of the handle (catch) in the “call stack” that existed whenthe handler was reached.Scheme has a more powerful concept that can be a little lessconvenient for exceptions:• You explicitly indicate what “handler” ( continuation) to transfercontrol to.• You do the transfer via a function application (that does not havefunction-application sem antics)• The continuation does not even have to be on the “call stack”when it’s transferred to!Dan Grossman CSE341 Fall 2004, Lecture 16 6'&$%Continuations for exce ptionsPlan:• Show how to use continuations for exce ptions (needed for hw 5).• Explain continuation-semantics “from scratch” (later)• Hint at some advanced uses (later)Syntax:(let/cc k e) ; bind k to ‘‘current continuation’’(k e) ; ‘‘invoke’’ continuation bound to kException idiom:• Instead of handler, use let/cc• Pass an appropriate function that invokes k to any function thatneeds to “raise”Dan Grossman CSE341 Fall 2004, Lecture 16


View Full Document

UW CSE 341 - Lecture Notes

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?