DOC PREVIEW
MIT 6 001 - Lecture Notes

This preview shows page 1-2-3-22-23-24-45-46-47 out of 47 pages.

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

Unformatted text preview:

6.001: Structure and Interpretation of Computer ProgramsData Types in Lisp/SchemeSymbolsHow do we refer to Symbols?Referring to SymbolsNew Special Form: quoteReview: data abstractionSymbol: a primitive typeWhat’s the difference between symbols and strings?The operation eq? tests for the same objectMaking list structure with symbolsMore Syntactic SugarSlide 13Slide 14Slide 15But wait… Clues about “guts” of SchemeSlide 18The Grimson Rule of Thumb for QuoteRevisit making list structure with symbolsAside: What all does the reader “know”?Traditional LISP structure: association listAlist operation: find-assocAn aside on testing equalityAlist operation: add-assocAlists are not an abstract data typeWhy do we care that Alists are not an ADT?Symbolic differentiationBuilding a system for differentiation1. How to get startedType of the data will guide implementation2. A direct implementationSimple expressionsCompound expressionsSum expressionsThe direct implementation works, but...3. A better implementationUse data abstractionsA better implementationIsolating changes to improve performanceModularity makes changes easierJust change data abstractionSeparating simplification from differentiationSimplifying sumsMore special cases in simplificationSpecial cases in simplifying productsSimplified derivative looks betterRecap6.001 SICP 1/496.001: Structure and Interpretation of Computer Programs•Symbols•Quotation•Relevant details of the reader•Example of using symbols•Alists•Differentiation6.001 SICP 2/49Data Types in Lisp/Scheme•Conventional•Numbers (integer, real, rational, complex)–Interesting property in “real” Scheme: exactness•Booleans: #t, #f•Characters and strings: #\a, “Hello World!”•Vectors: #(0 “hi” 3.7)•Lisp-specific•Procedures: value of +, result of evaluating ( (x) x)•Pairs and Lists: (3 . 7), (1 2 3 5 7 11 13 17)•Symbols: pi, +, MyGreatGrandMotherSue6.001 SICP 3/49Symbols•So far, we’ve seen them as the names of variables•But, in Lisp, all data types are first class•Therefore, we should be able to–Pass symbols as arguments to procedures–Return them as values of procedures–Associate them as values of variables–Store them in data structures–E.g., (apple orange banana)apple orangebanana6.001 SICP 4/49How do we refer to Symbols?•Substitution Model’s rule of evaluation:•Value of a symbol is the value it is associated with in the environment•We associate symbols with values using the special form define–(define pi 3.1415926535)•… but that doesn’t help us get at the symbol itself6.001 SICP 5/49Referring to Symbols•Say your favorite color•Say “your favorite color”•In the first case, we want the meaning associated with the expression, e.g., •red•In the second, we want the expression itself, e.g., •your favorite color•We use quotation to distinguish our intended meaning6.001 SICP 6/49New Special Form: quote•Need a way of telling interpreter: “I want the following object as whatever it is, not as an expression to be evaluated”(quote alpha);Value: alpha(define pi 3.1415926535);Value: "pi --> 3.1415926535"pi;Value: 3.1415926535(quote pi);Value: pi(+ pi pi);Value: 6.283185307(+ pi (quote pi));The object pi, passed asthe first argument to integer->flonum, is not the correct type.(define fav (quote pi))fav;Value: pi6.001 SICP 7/49•A data abstraction consists of:•constructors •selectors •operations •contractReview: data abstraction(define make-point (lambda (x y) (list x y)))(define x-coor (lambda (pt) (car pt)))(define on-y-axis? (lambda (pt) (= (x-coor pt) 0)))(x-coor (make-point <x> <y>)) = <x>6.001 SICP 8/49Symbol: a primitive type•constructors: None since really a primitive, not an object with parts•Only way to “make one” is to type it–(or via string->symbol from character strings, but shhhh…)•selectors None–(except symbol->string)•operations: symbol? ; type: anytype -> boolean (symbol? (quote alpha)) ==> #t eq? ; discuss in a minuteR5RS shows thefull riches of Scheme6.001 SICP 9/49What’s the difference between symbols and strings?•Symbol•Evaluates to the value associated with it by define•Every time you type a particular symbol, you get the exact same one! Guaranteed.–Called interning•E.g., (list (quote pi) (quote pi))•String•Evaluates to itself•Every time you type a particular string, it’s up to the implementation whether you get the same one or different ones.•E.g., (list ”pi” ”pi”)orpi”pi””pi””pi”6.001 SICP 10/49The operation eq? tests for the same object •a primitive procedure•returns #t if its two arguments are the same object•very fast (eq? (quote eps) (quote eps)) ==> #t (eq? (quote delta) (quote eps)) ==> #f•For those who are interested:; eq?: EQtype, EQtype ==> boolean; EQtype = any type except number or string•One should therefore use = for equality of numbers, not eq?6.001 SICP 11/49Making list structure with symbols((red 700) (orange 600) (yellow 575) (green 550)(cyan 510) (blue 470) (violet 400))(list (list (quote red) 700) (list (quote orange) 600) … (list (quote violet) 400))red 700orange 600violet 4006.001 SICP 12/49More Syntactic Sugar•To the reader,’piis exactly the same as if you had typed(quote pi)•Remember REPL'pi;Value: piUser types’pi(quote pi)readpievalpiprint6.001 SICP 13/49More Syntactic Sugar•To the reader,’piis exactly the same as if you had typed(quote pi)•Remember REPL'pi;Value: pi'17;Value: 17'"hi there";Value: "hi there"User types’17(quote 17)read17eval17print6.001 SICP 14/49More Syntactic Sugar•To the reader,’piis exactly the same as if you had typed(quote pi)•Remember REPL'pi;Value: pi'17;Value: 17'"hi there";Value: "hi there"'(+ 3 4);Value: (+ 3 4)User types’(+ 3 4)(quote (+ 3 4))read(+ 3 4)eval(+ 3 4)print6.001 SICP 15/49More Syntactic Sugar•To the reader,’piis exactly the same as if you had typed(quote pi)•Remember REPL'pi;Value: pi'17;Value: 17'"hi there";Value: "hi there"'(+ 3 4);Value: (+ 3 4)''pi;Value: (quote pi)But in Dr. Scheme,'piUser types’’pi(quote (quote pi))read(quote pi)eval(quote pi)print6.001 SICP 16/49But wait… Clues about “guts” of Scheme(pair? (quote (+ 2 3)));Value: #t(pair? '(+ 2 3));Value: #t(car '(+ 2 3));Value: +(cadr '(+ 2 3));Value: 2(null? (cdddr '(+ 2 3)));Value: #t+ 2 3Now we know that expressions are represented by lists!6.001 SICP


View Full Document

MIT 6 001 - Lecture Notes

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 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?