'&$%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