Introduction to Scheme { a Language Is Described by Specifying Its Syntax and Semantics { Syntax: the Rules for Writing Programs

Introduction to Scheme { a Language Is Described by Specifying Its Syntax and Semantics { Syntax: the Rules for Writing Programs

How do you describe them Introduction to Scheme { A language is described by specifying its syntax and semantics { Syntax: The rules for writing programs. z We will use Context Free Grammars. Gul Agha { Semantics: What the programs mean. z Informally, using English CS 421 z Operationally, showing an interpreter Fall 2006 z Other: Axiomatic, Denotational, … { Look at R5RS for how this is done formally CS 421 Lecture 2 & 3: Scheme 2 Context Free Grammars Example: English Language A (context-free) grammar consists of four { Terminals parts: 1. A set of tokens or terminals. These are the atomic symbols in the language. { Non-Terminals 2. A set of non-terminals. These represent the various concepts in the language 3. A set of rules called productions { Productions mapping the non-terminals to groups of tokens. { 4. A start non-terminal Main Non-Terminal CS 421 Lecture 2 & 3: Scheme 3 CS 421 Lecture 2 & 3: Scheme 4 Example: English Language Example: English Language { Terminals { Terminals z Words of the language z Words of the language { Non-Terminals { Non-Terminals z sentences, verbs, nouns, adverbs, complex, noun phrases, ..... { Productions { Productions { Main Non-Terminal { Main (Start) Non-Terminal CS 421 Lecture 2 & 3: Scheme 5 CS 421 Lecture 2 & 3: Scheme 6 1 Example: English Language Backus Naur Form { Terminals { BNF non-terminals are enclosed in the z Words of the language symbols < … > { Non-Terminals { < > represents the empty string z sentences, verbs, nouns, adverbs, { Example: complex, noun phrases, ..... <string> ::= < > | <char><string> { Productions <char> ::= a | b | c | ..... z Any guesses ::= should be read as can be { Main Non-Terminal | should be read as or z sentence Sometimes Æ is used for ::= CS 421 Lecture 2 & 3: Scheme 7 CS 421 Lecture 2 & 3: Scheme 8 A Grammar for Core Scheme Grammar continued <expression> Æ <constant> Æ <boolean> <constant> | <number> | <character> | <identifier> | <string> | (if <expression> <expression> <expression>) | (define <identifier> <expression>) <formals> Æ (<variable>*) | (quote <expression>) | <variable> | <(<variable >…<variable >. <variable>) | (begin <expression> ... <expression>) 1 n | (lambda <formals> <body>) <body> Æ <expression> | <procedure call> <procedure call> Æ (<operator> <operand>*) <operator> Æ <expression> <operand> Æ <expression> And productions for <boolean>, <number>, <character>, <string> CS 421 Lecture 2 & 3: Scheme 9 CS 421 Lecture 2 & 3: Scheme 10 Syntactic Keywords of Scheme An Overview of Scheme { Control: quote, lambda, if, define, { R-E-P begin. z Interacting via the Read-Eval-Print loop { Atomic Data { Predicates: equal? null? zero? z booleans, numbers, symbols, characters & ... strings ... { Expressions { Recognizers: integer? boolean? z What are they? and how are they evaluated. string? pair? list? procedure? ... { Conditionals { Lists: car cdr cons ... z if and syntactic sugar (macros): not, and, or, cond. { Recognizers & Predicates CS 421 Lecture 2 & 3: Scheme 11 CS 421 Lecture 2 & 3: Scheme 12 2 Overview of Scheme (Contd.) Overview of Scheme (contd.) { Functions { Recursive Definition z lambda, let, let* z what is it, some examples, and how it works. { Definitions { Recursion vs Iteration z what they are and what they look like. z translating a while into a recursive function (tail recursion) { Lists & quote z what they look like, including the empty list { List Processing z some world famous list functions { List Manipulation z Constructing and deconstructing lists: car, cdr, { Functionals cons z functions of function e.g., mapping & sorting CS 421 Lecture 2 & 3: Scheme 13 CS 421 Lecture 2 & 3: Scheme 14 Binding Functional Language { Pattern Matching Functions are first-class values in Scheme.. z what is it & how is it done. { Functions can be used as arguments { Notions of Equality { Functions can be returned as values z eq? and its many relations. { Functions can be bound to variables and { Data Mutation & Scheme Actors stored in data structures z setcar!, setcdr! and the sieve of erasthones Important Note: No mutations Scheme is not a strictly functional (assignments) may be used in the first language: bindings of variables can be couple of MPs mutated. We will avoid this for now. CS 421 Lecture 2 & 3: Scheme 15 CS 421 Lecture 2 & 3: Scheme 16 Talking to Scheme > (define cell (cons + *)) > 3434 > (+ 1 2 3 4) > cell 3434 24 (cons + *) > (integer? 2345) > + #t + #<primitive-procedure *> > (procedure? (car cell)) > (+ 1 2 3 4) > (lambda (x) (* x x)) true 10 (lambda (a1) …) #<CLOSURE (x) (* x x)> > (define + *) > ((cdr cell) 5 5) #<unspecified> ¾ ((lambda (x) (* x x)) 12) 144 25 CS 421 Lecture 2 & 3: Scheme 17 CS 421 Lecture 2 & 3: Scheme 18 3 Atomic Data Notes { Booleans { Characters & Strings { Booleans, numbers, characters are z the truth values: z #\a #f #t constants. ;lower case letter a { Numbers z #\space ; the preferred { Symbols (other than predefined z integer way to write a space functions) must be given a value. z rational z #\newline z real ; the newline { All these data types are disjoint, i.e. z complex number character nothing can be both a this and a { Symbols or z "this is a typical string“ identifiers that. z Used as variables to name values CS 421 Lecture 2 & 3: Scheme 19 CS 421 Lecture 2 & 3: Scheme 20 Expressions Examples { Constants, Symbols and Data > (* 3 4 (+ 2 3)) { Lists 60 z Special forms > (*) z Application of a function to given arguments 1 <exp> ::= (<exp> <exp> ... <exp>) > ((lambda (x) (* x 4)) 4) Stands for: <function application> ::= 16 (<fun> <arg1> ... <argn>) > ((car (cons + *)) 7 3) 10 CS 421 Lecture 2 & 3: Scheme 21 CS 421 Lecture 2 & 3: Scheme 22 Notes Sequencing (begin <exp> ..... <exp>) { Function application is call-by-value. { evalutes the expressions <exp> from left right Each <arg> is evaluated, as is the { returns the value of the rightmost expression. <fun>. >(begin (write "a") (write "b") { The order of evaluation is not (write "c") (+ 2 3)) specified. "a""b""c"5 { Some functions have no arguments. z writes "a", "b" and "c" out to the screen, in { ( ) is not a function application, its that order. the empty list. z evaluates to 5. CS 421 Lecture 2 & 3: Scheme 23 CS 421 Lecture 2 & 3: Scheme 24 4 Lists Lists and Expressions ¾ (list? x) { Scheme expressions are lists ¾ evaluates to #t if and only if x is/names a list. { Expressions are evaluated ¾ Otherwise it evaluates to #f. { How to say something is a list rather than an expression? ¾ ( ) ;the empty list { Use the special form quote: ¾ (list? ( )) > (quote <expression>) ¾ (null? ()) z evaluates to the list or scheme object expression. ¾ (null? x) z No evaluation of expression takes place. ¾ (this is a list with a very deep z Abbreviated as: ((((((((((((((((((end))))))))))))))))))) ’<expression> CS 421 Lecture 2 & 3: Scheme 25 CS 421 Lecture 2 & 3: Scheme 26 Examples Notes { Notation Convention >’(+ 1 2) z Recognizers end with a ? z There is also a ! convention -- used for something different >’(+ ’(* 3 4) 1 2) { Constants Convention z Constants such as booleans, strings, characters, and numbers do not need to be quoted. >(procedure? ’+) { Quoted Lists >(procedure? +) z A quoted list is treated as a constant. It is an error to alter one. >(symbol? ’+) { Nested Lists >(symbol? +) z Lists may contain lists, which may contain lists, which may contain lists .... CS 421 Lecture 2 & 3: Scheme 27 CS 421 Lecture 2 & 3: Scheme 28 List Manipulation Operations Examples { cons is the list constructor 1. (cons 1 ()) evaluates to (1) { car gets the first element of a list. 2. (cons 5 ’(1 2 3 4)) z Its name comes from: 3. (cons ’(1 2 3 4) ’(5)) Contents of the AddressRegister. 4. (car ’(a b c d)) { cdr gets the rest of a list. 5. (cdr ’(a b c d)) z Its name comes from: 6. (caar ’((1) 2 3 4)) Contents of the Decrement Register. 7. (cddr ’(a b c d)) { Names inherited from the first implementation of 8. (cdddr ’(a b c d)) Lisp on IBM 704 back in 1958. 9. (cddddr ’(a b c d)) CS 421 Lecture 2 & 3: Scheme 29 CS 421 Lecture 2 & 3: Scheme 30 5 Notes Lisp Manipulation Axioms 1. (car ()) is undefined, as is (cdr ()). 1. (car (cons <a> <d>)) is <a> 2. (pair? <list>) => #t when <list> is non-empty. 3. (pair? ()) 2. (cdr (cons <a> <d>)) is <d> 4. Any composition of cars and cdrs of length upto 3. If <list> is a list, then length 4 is valid, z car cdr (cons (car <list>) (cdr <list>)) z caar cdar cadr cddr z caaar cdaar cadar caddr cdadr cddar cdddr is equal? to <list> z caaaar ......... cddddr Question: How many operations in this last line? CS 421 Lecture 2 & 3: Scheme 31 CS 421 Lecture 2 & 3: Scheme 32 List Recursion Think Recursively! { Check if it is a list (list? …) Write a function, F, which when { Repeatedly apply cdr till you reach given a list <list> produces a the end of the list new list, obtained by adding 1 { Use (null? …) to check if it empty to every number in the <list> before deconstructing { Use (cons …) to build a list ¾ (F ’(a 1 b 2 c 3 d 4 e 5)) { Use (cdr …) to walk through list (F ’(a 2 b 3 c 4 d 5 e 6)) CS 421 Lecture 2 & 3: Scheme 33 CS 421 Lecture 2 & 3: Scheme 34 Base Case and Induction The Answers { If <list> is the empty list, then { (F ’( )) should just be ( ) what should (F <list>) be? { if (car <list>) is a number { If I know what (car <list>) is, and I (F <list>) should be also know what (F (cdr <list>)) is, (cons (+ 1 (car <list>)) (F (cdr do I know what (F <list>) should <list>))) be? { if (car <list>) is not a number (F <list>) should be (cons (car <list>) (F (cdr <list>))) CS 421 Lecture 2 & 3: Scheme 35 CS 421 Lecture 2 & 3: Scheme 36 6 The Code Tracing Procedures (define F > (F '(a 1 b 2 c 3 d 4 e 5)) > (trace F) (lambda (l) (a 2 b 3 c 4 d 5 e 6) (F) (if (null? l) ( ) > (F '(1 2 3 4)) | | |() (if (number? (car l)) |(F (1 2 3 4)) | | (5) (cons (+ 1 (car l)) | (F (2 3 4)) | |(4 5) (F (cdr l))) | |(F (3 4)) | (3 4 5) (cons (car l) | | (F (4)) |(2 3 4 5) (F (cdr l))))))) | | |(F ()) (2 3 4 5) CS 421 Lecture 2 & 3: Scheme 37 CS 421 Lecture 2 & 3: Scheme 38 Understanding cons Box Diagrams •cons creates a new pair each time it is called! List notation Pair notation •eq? on pairs returns true if and only if the two pairs are the same pairs (a b c d) (a .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us