DOC PREVIEW
UW CSE 341 - Lecture Notes

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 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 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 341:Programming LanguagesDan GrossmanFall 2004Lecture 3— Let bindings, options, and benefits of no mutationDan Grossman CSE341 Fall 2004, Lecture 3 1'&$%Let bindingsMotivation: Functions without local variables can be poor style and/orreally inefficient.Syntax: let b1 b2 ... bn in e end where each bi is a binding.Typing rules: Type-check each bi and e in context including previousbindings. Type of whole expression is type of e.Evaluation rules: Evaluate each bi and e in environment includingprevious bindings. Value of whole expression is result of evaluating e.Elegant design worth repeating:• Let-expressions can appear anywhere an expression can.• Let-expressions can have any kind of binding.– Local functions can refer to any bindings in scope.Dan Grossman CSE341 Fall 2004, Lecture 3 2'&$%More than styleExercise: hand-evaluate bad_max and good_max for lists [1,2][1,2,3], and [3,2,1].Dan Grossman CSE341 Fall 2004, Lecture 3 3'&$%Summary and general patternMajor progress: recursive functions, pairs, lists, let-expressionsEach has a syntax, typing rules, evaluation rules.Functions, pairs, and lists are very diffe rent, but we can describe themin the same way:• How do you create values? (function definition, pair expressions,empty-list and ::)• How do you use values? (function application, #1 and #2, null,hd, and tl)This (and conditionals) is enough for your homework though:• andalso and orelse help• You need options (next slide)• Soon: much better ways to use pairs and lists (pattern-matching)Dan Grossman CSE341 Fall 2004, Lecture 3 4'&$%Options“Options are like lists that can have at most one element.”• Create a t option with NONE or SOME e where e has type t.• Use a t option with isSome and valOfWhy not just use (more general) lists? An interesting style trade-off:• Options better express purpose, enforce invariants on callers,maybe faster.• But cannot use functions for list s already written.Dan Grossman CSE341 Fall 2004, Lecture 3 5'&$%You want to change something?There is no way to mutate (assign to) a binding, pair component, orlist element.How could the lack of a feature make programming easier?In this case:• Amount of sharing is indistinguishable– Aliasing irrelevant to correctness!• Bindings are invariant across function application– Mutation breaks c ompositional reasoning, a (the?) intellectualtool of engineeringDan Grossman CSE341 Fall 2004, Lecture 3


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?