DOC PREVIEW
UW CSE 341 - Racket

This preview shows page 1-2-24-25 out of 25 pages.

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

Unformatted text preview:

Slide 1RacketRacket vs. SchemeGetting startedExampleSome nicetiesOld-friend #1: curryingOld-friend #2: List processingRacket syntaxWhy is this good?Parenthesis biasSlide 12Parentheses matterExampleDynamic typingExampleBetter styleA variationLocal bindingsLetLet*LetrecMore letrecLocal definesTop-levelCSE341: Programming LanguagesLecture 14Introduction to RacketDan GrossmanFall 2011RacketNext 2+ weeks will use the Racket language (not ML) and the DrRacket programming environment (not emacs)–Installation / basic usage instructions on course website•Like ML, functional focus with imperative features–Anonymous functions, closures, no return statement, etc.–But doesn’t rely on pattern-matching•Unlike ML, no static type system: accepts more programs, but most errors do not occur until run-time•Really minimalist syntax•Advanced features like macros, modules, quoting/eval, continuations, contracts, …–Will do only a couple of theseFall 2011 2CSE341: Programming LanguagesRacket vs. Scheme•Scheme and Racket are very similar languages–Racket “changed its name” in 2010–Notes and instructor may occasionally slip up•Racket made some non-backward-compatible changes…–How the empty list is written–Cons cells not mutable–How modules work–Etc.… and many additions•Result: A modern language used to build some real systems–More of a moving target (notes may become outdated)–Online documentation, particular “The Racket Guide”Fall 2011 3CSE341: Programming LanguagesGetting startedDrRacket “definitions window” and “interactions window” very similar to how we used emacs and a REPL–DrRacket has always focused on good-for-teaching–See usage notes for how to use REPL, testing files, etc.•You need to get good at learning new tools on your own, but today’s demos (more code than in slides) will helpStart every file with a line containing only#lang racket(Can have comments before this, but not code)A file is a module containing a collection of definitions (bindings)…Fall 2011 4CSE341: Programming LanguagesExampleFall 2011 5CSE341: Programming Languages#lang racket(define x 3) (define y (+ x 2)) (define cube ; function (lambda (x) (* x (* x x)))) (define pow ; recursive function (lambda (x y) (if (= y 0) 1 (* x (pow x (- y 1))))))Some nicetiesMany built-in functions (a.k.a. procedures) take any number of args –Yes * is just a function–Yes we’ll show you later how to define variable-arity functionsBetter style for non-anonymous function definitions (just sugar):Fall 2011 6CSE341: Programming Languages(define cube (lambda (x) (* x x x)))(define (cube x) (* x x x))(define (pow x y) (if (= y 0) 1 (* x (pow x (- y 1)))))Old-friend #1: curryingCurrying is an idiom that works in any language with closures–Less common in Racket because it has real multiple argsFall 2011 7CSE341: Programming Languages(define pow (lambda (x) (lambda (y) (if (= y 0) 1 (* x ((pow x) (- y 1)))))))(define three-to-the (pow 3))(define eightyone (three-to-the 4))(define sixteen ((pow 2) 4))Sugar for defining curried functions: (No sugar for calling curried functions)(define ((pow x) y) (if …Old-friend #2: List processingEmpty list: null (unlike Scheme, () doesn’t work, but '() does)Cons constructor: cons (also (list e1 … en) is convenient)Access head of list: car (car and cdr a historical accident)Access tail of list: cdrCheck for empty: null?Examples: Fall 2011 8CSE341: Programming Languages(define (sum xs) (if (null? xs) 0 (+ (car xs) (sum (cdr xs)))))(define (my-append xs ys) (if (null? xs) ys (cons (car xs) (my-append (cdr xs) ys))))Racket syntaxIgnoring a few bells and whistles, Racket has an amazingly simple syntaxA term (anything in the language) is either:–An atom, e.g., #t, #f, 34, "hi", null, 4.0, x, …–A special form, e.g., define, lambda, if•Macros will let us define our own–A sequence of terms in parens: (t1 t2 … tn)Note: Can use [ anywhere you use (, but must match with ]–Will see shortly places where […] is common style–DrRacket lets you type ) and replaces it with ] to matchFall 2011 9CSE341: Programming LanguagesWhy is this good?By parenthesizing everything, converting the program text into a tree representing the program (parsing) is trivial and unambiguous–Atoms are leaves–Sequences are nodes with elements as children–(No other rules)Also makes indentation easyExample:Contrast CSE142’s obsession with expression precedenceFall 2011 10CSE341: Programming Languages(define cube (lambda (x) (* x x x)))definecube lambdax *xx xParenthesis bias•If you look at the HTML for a web page, it takes the same approach:–(foo written <foo>–) written </foo>•But for some reason, LISP/Scheme/Racket is the target of subjective parenthesis-bashing–Bizarrely, often by people who have no problem with HTML–You are entitled to your opinion about syntax, but a good historian wouldn’t refuse to study a country where he/she didn’t like people’s accentsFall 2011 11CSE341: Programming LanguagesFall 2011 12CSE341: Programming Languageshttp://xkcd.com/297/LISP invented around 1959 by John McCarthy (9/4/27-10/23/2011)•Invented garbage collectionParentheses matterYou must break yourself of one habit for Racket: –Do not add/remove parens because you feel like it •Parens are never optional or meaningless!!!–In most places (e) means call e with zero arguments–So ((e)) means call e with zero arguments and call the result with zero argumentsWithout static typing, often get hard-to-diagnose run-time errorsFall 2011 13CSE341: Programming LanguagesExampleCorrect: Treats 1 as a zero-argument function (run-time error):Gives if 5 arguments (syntax error)3 arguments to define (including (n)) (syntax error)Treats n as a function, passing it * (run-time error)Fall 2011 14CSE341: Programming Languages(define (fact n)(if (= n 0) 1 (* n (fact (- n 1)))))(define (fact n)(if (= n 0) (1)(* n (fact (- n 1)))))(define (fact n)(if = n 0 1 (* n (fact (- n 1)))))(define fact (n)(if (= n 0) 1 (* n (fact (- n 1)))))(define (fact n)(if (= n 0) 1 (n * (fact (- n 1)))))Dynamic typingWill spend a later lecture contrasting static typing (e.g., ML) with dynamic typing (e.g., Racket)For now:–Frustrating not to catch “little errors” like (n * x) until you test your function–But can use very flexible data structures and code without


View Full Document

UW CSE 341 - Racket

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Racket
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 Racket 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 Racket 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?