lecture 24 programming languages and programs
play

Lecture #24: Programming Languages and Programs Metalinguistic - PDF document

Lecture #24: Programming Languages and Programs Metalinguistic Abstraction A programming language is a notation for describing computations Weve created abstractions of actionsfunctionsand of things or processes. classes.


  1. Lecture #24: Programming Languages and Programs Metalinguistic Abstraction • A programming language is a notation for describing computations • We’ve created abstractions of actions—functions—and of things— or processes. classes. • These range from low-level notations, such as machine language or • Metalinguistic abstraction refers to the creation of languages— simple hardware description languages, where the subject matter is abstracting description. Programming languages are one example. typically finite bit sequences and primitive operations on them that • Programming languages are effective: they can be implemented. correspond directly to machine instructions or gates, . . . • These implementations interpret utterances in that language, per- • . . . To high-level notations, such as Python, in which the subject mat- forming the described computation or controlling the described pro- ter can be objects and operations of arbitrary complexity. cess. • They may be general-purpose , such as Python or Java, or domain- • The interpreter may be hardware (interpreting machine-language specific , specialized to particular purposes, such as CSS or XAML. programs) or software (a program called an interpreter ), or (in- • Their implementations may stand alone (as for most implementations creasingly common) both. of Python or C), or be embedded as a component of a larger system. • To be implemented, though, the grammar and meaning of utterances • The universe of implementations of these languages is layered: Python in the programming language must be defined precisely. can be implemented in C, which in turn can be implemented in assem- bly language, which in turn is implemented in machine language, which in turn is implemented with gates. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 1 Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 2 Review (from Lecture 1): What’s In A Programming The Scheme Language Language? Scheme is a dialect of Lisp: • Values: the things programs fiddle with; • “The only programming language that is beautiful.” —Neal Stephenson • Primitive operations (on values); • “The greatest single programming language ever designed” • Combining mechanisms: glue operations together; —Alan Kay • Predefined names (the “library”); • Definitional mechanisms: which allow one to introduce symbolic names and (in effect) to extend the library. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 3 Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 4 Scheme Background Values • Invented in the 1970s by Guy Steele (“The Great Quux”), who has • We divide Scheme data into atoms and pairs . also participated in the development of Emacs, Java, and Common • The classical atoms: Lisp. – Numbers: integer, floating-point, complex, rational. • Designed to simplify and clean up certain irregularities in Lisp di- – Symbols. alects at the time. – Booleans: #t, #f. • Used in a fast Lisp compiler (Rabbit). – The empty list: (). • Still maintained by a standards committee (although both Brian Har- – Procedures (functions). vey and I agree that recent versions have accumulated an unfortu- • Some newer-fangled, mutable atoms: nate layer of cruft). – Vectors: Python lists. – Strings. – Characters: Like Python 1-element strings. • Pairs are two-element tuples, where the elements are (recursively) Scheme values. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 5 Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 6

  2. Symbols Pairs and Lists • Lisp was originally designed to manipulate symbolic data: e.g., for- • As we’ve seen, one can build practically any data structure out of mulae as opposed merely to numbers. pairs. • Such data is typically recursively defined (e.g., “an expression con- • The Scheme notation for the pair of values V 1 and V 2 is sists of an operator and subexpressions”). ( V 1 . V 2 ) • The “base cases” had to include numbers, but also variables or words. • In Scheme, the main use of pairs is to build lists , defined recursively • For this purpose, Lisp introduced the notion of a symbol: like an rlist: – Essentially a constant string. – The empty list, written “()”, is a list. – Two symbols with the same “spelling” (string) are always the same – The pair consisting of a value V and a list L is a list that starts object. with V , and whose tail is L . – Confusingly, the reader (the program that reads in Scheme pro- • Lists are so prevalent that there is a standard abbreviation: You can grams and data) converts symbols it reads into lower-case first. write ( V . ()) as ( V ), and (. . . . ( V . R )) as ( . . . V . R ) . • The main operation on symbols, therefore, is equality. • By repeated application of these rules, the typical list: ( V 1 . ( V 2 . ( . . . ( V n . ()) . . . ))) becomes just ( V 1 V 2 . . . V n ) . Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 7 Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 8 Programs Quotation • Scheme expressions programs are instances of Lisp data structures • Since programs are data, we have a problem: suppose you want your program to create a piece of data that happens to look like a pro- (“Scheme is written in Scheme”). gram? • At the bottom, numerals, booleans, characters, and strings are ex- • How do we say, for example, “Set the variable x to the three- pressions that stand for themselves. element list (+ 1 2)” without it meaning “Set the variable x to the • Most lists stand for function calls: value 3?” ( OP E 1 · · · E n ) • The “quote” special form does this: evaluating (quote E) yields E as a Scheme expression means “evaluate OP and the E 1 (recursively), itself as the value, without treating it like a Scheme expression to and then apply the value of OP , which must be a function, to the be evaluated. values of the arguments E i .” >>> (+ 1 2) 3 • A few lists, identified by their OP , are special forms , which each >>> (quote (+ 1 2)) have different meanings. (+ 1 2) >>> ’(+ 1 2) ; Shorthand. Converted to (quote (+ 1 2)) (+ 1 2) • How about >>> (quote (1 2 ’(3 4))) ;? (1 2 (quote (3 4))) Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 9 Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 10 Symbols Function Evaluation • When evaluated as a program, a symbol acts like a variable name. • Function evaluation is just like Python: same environment frames, same rules for what it means to call a user-defined function. • Variables are bound in environments, just as in Python, although the syntax differs. • To create a new function, we use the lambda special form: • To define a new symbol, either use it as a parameter name (later), >>> ( (lambda (x y) (+ (* x x) (* y y))) 3 4) or use the “define” special form: 25 >>> (define fib (define pi 3.1415926) (lambda (n) (if (< n 2) n (+ (fib (- n 2) (- n 1)))))) (define pi**2 (* pi pi)) >>> (fib 5) • This (re)defines the symbols in the current environment. The sec- 5 ond expression is evaluated first. • The last is so common, there’s an abbreviation: • To assign a new value to an existing binding, use the set! special >>> (define (fib n) form: (if (< n 2) n (+ (fib (- n 2) (- n 1))))) (set! pi 3) • Here, pi must be defined, and it is that definition that is changed (not like Python). Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 11 Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend