Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.

Similar presentations


Presentation on theme: "Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely."— Presentation transcript:

1 Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely different…” – Monty Python

2 Intro to Scheme2 Functional Languages  Scheme is a functional language. It belongs to a class of languages that differ from imperative languages in a number of ways: Elegant, concise, syntax (and specification). Definition of functions is paramount – Order is not that important! No iteration! -- Use of recursion instead. Data is generally weakly/dynamically typed.

3 Intro to Scheme3 Why Scheme?  Very simple syntax (the E-BNF specification is only 8 pages – see the last slides)  Lisp is the standard for functional languages, Scheme is a rising star  Many efforts to make it a teachable language Cal-Tech, MIT, etc.  DR-Scheme

4 Intro to Scheme4 Forms and Evaluation  The basic scheme construct is called a form (function argument1 argument2 argument3…)  (+ 2 5) » 7  (+ 3 4 5) » 12  (/ 22 7) » 22/7  (number? 5) » #t  (boolean? 5) » #f  (boolean? #5) » #t  (eqv? 42 42) » #t  (eqv? 42 42.0) » #f  (= 42 42.0) » #t  (= 42 #x2A) » #t  (expt 4 2) » 16  (expt 4 1/2) » 2  (max 1 3 5 7) » 7  (min 1 3 5 7) » 1  (abs -2) » 2  (abs -2 5)  abs: expects 1 argument, given 2: -2 5

5 Intro to Scheme5 Substitution Model The basic rule: To evaluate a Scheme expression: 1.Evaluate its operands 2.Evaluate the operator 3.Apply the operator to the evaluated operands

6 Intro to Scheme6 Warning! Boredom ahead!  We will walk through evaluations in painful, excruciating detail  Not the most exciting part of course  but necessary to give a precise model

7 Intro to Scheme7 Example expression eval  Example: (+ 3 (* 4 5) )

8 Intro to Scheme8 Example expression eval  Example: (+ 3 (* 4 5) ) operator

9 Intro to Scheme9 Example expression eval  Example: (+ 3 (* 4 5) ) operator operand 1

10 Intro to Scheme10 Example expression eval  Example: (+ 3 (* 4 5) ) operator operand 1 operand 2

11 Intro to Scheme11 Example expression eval  Example: (+ 3 (* 4 5)) evaluate 3 evaluate (* 4 5)  evaluate 4  evaluate 5  evaluate *  apply * to 4, 5  20 evaluate + apply + to 3, 20  23

12 Intro to Scheme12 Primitive expressions  Numbers evaluate to themselves 105  105  Primitive procedures +, -, *, /, abs … evaluate to the corresponding internal procedure e.g. "+" evaluates to the procedure that adds numbers together  Can write this as: +  + or as: +  [primitive procedure +] write this out explicitly for now

13 Intro to Scheme13 Another example  (+ 3 (- 4 (* 5 (expt 2 2)))) evaluate 3  3 evaluate (- 4 (* 5 (expt 2 2)))  evaluate 4  4  evaluate (* 5 (expt 2 2)) –evaluate 5  5 –evaluate (expt 2 2) »evaluate 2  2, evaluate 2  2, expt  expt »apply expt to 2, 2  4 –evaluate *  * –apply * to 5, 4  20  apply - to 4, 20  -16 apply + to 3, -16  -13

14 Intro to Scheme14 Evaluation with variables  An assignment provides an association between a symbol (variable) and its value (define x 3)  Here x is the variable, 3 is the value  To evaluate a variable:  look up the value associated with the variable  and replace the variable with its value

15 Intro to Scheme15 Variable evaluation example  (define x 3)  Then evaluate x look up value of x x has value 3 (due to define) result: 3

16 Intro to Scheme16 Simple expression evaluation  Assignment and evaluation (define x 3) (define y 4)  evaluate (+ x y)  evaluate x  3  evaluate y  4  evaluate +  [primitive procedure +]  apply + to 3, 4  7

17 Intro to Scheme17 Special forms  N.B. There are a few special forms which do not evaluate in the way we’ve described.  define is one of them (define x 3) We do not evaluate x before applying define to x and 3 Instead, we  evaluate the second operand (3  3)  make an association between it and the first operand (x)

18 Intro to Scheme18 Special forms  Another example: (define x (+ 2 3))  Evaluate (+ 2 3) evaluate 2  2 evaluate 3  3 evaluate +  [primitive procedure +] apply + to 2, 3  5  Make association between x and 5 details of how association is made aren't important

19 Intro to Scheme19 Basic Data Types  Numbers: 3, -3, 22/7, 3+2i, #xF3A, #o721, #b110110, 3.1416 (= 42 #x2A) » #t  Single characters: #\c, #\newline, #\tab, #\space (char? #\c), (char=? #\c #\b), (char<=? #\c #\b)  Strings: “Hello World” (string #\h #\i) (string-ref “Hello” 1) » #\e (string-append “E” “Pluribus” “Unum”) » “E Pluribus Unum”

20 Intro to Scheme20 More types  Procedures (lambda)  Booleans (#t and #f)

21 Intro to Scheme21 Symbols  Symbols have two parts, a name and a value Similar to variables in other languages, but beware of the many differences! We’ll see a lot more about the differences later. (define xyz 9)  xyz » 9  (symbol? xyz) » #f  (symbol? ’xyz) » #t  (set! xyz 8)  (set! qrs 7)  set!: cannot set undefined identifier: qrs

22 Intro to Scheme22 Arbitrary Data Structures  What is the simplest data structure that you can use to create any other data structure, including dynamically sized structures?  What is the smallest component of this structure?

23 Intro to Scheme23 Dotted Pairs  Scheme uses pairs for many, many things Programs (forms) Arguments Data structures  A pair is a two-cell node. Each cell contains a pointer to a value or another pair.  A node with two pointers is called a dotted pair (cons 1 2) » (1. 2) ’(1. 2) » (1. 2) 21

24 Intro to Scheme24 car and cdr  You access the two parts of a dotted pair with the accessors car and cdr : (define x (cons 1 2))  (car x) » 1  (cdr x) » 2  (pair? a) returns true if a is a dotted pair (pair? (cons 3 4)) » #t (pair? 3) » #f

25 Intro to Scheme25 Dotted pairs can be nested  (define x (cons (cons 1 2) (cons 3 4))  (define x ‘((1. 2). (3. 4))) (car x) » (1. 2) (cdr x) » (3. 4) 4321

26 Intro to Scheme26 Scheme shorthand  (cons 1 (cons 2 (cons 3 (cons 4 5)))) » (1 2 3 4. 5)  (1 2 3 4. 5) is an abbreviation for (1. (2. (3. (4. 5)))) 12345

27 Intro to Scheme27 Lists  ’(1 2 3 4) » (1 2 3 4)  (1 2 3 4) is an abbreviation for (1. (2. (3. (4. ()))))  Just like a standard linked list An empty list, ’(), is essentially a null pointer  This is often how parameters are passed, etc. This special construct of nested dotted pairs is called a list 1234

28 Intro to Scheme28 List() (list 1 2 3 4 5) Is the same as… (cons 1 (cons 2 (cons 3 (cons 4 cons 5 nil)))) Both produce: (1. (2. (3. (4. (5. ()))))) Or, as scheme would print it… (1 2 3 4 5)

29 Intro to Scheme29 car and cdr  The car of a list is a value  The cdr of a list is a list  Special names: cadr, cddr, cdar, etc. (define x ’(1 2 3 4 5)  (car x) » 1  (cdr x) » (2 3 4 5)  (cadr x) » 2  (cddr x) » (3 4 5)

30 Intro to Scheme30 Quote and Lists  ‘(item1, item2, item3, …) is syntactic sugar for (list ‘item1 ‘item2 ‘item3 …)  ‘(1 2 3 4 5) »(1 2 3 4 5)  ‘(a b c d e) »(a b c d e)  ‘(1 2 3 4 5)  procedure application: expected procedure, given: 1; arguments were: 2 3 4 5

31 Intro to Scheme31 Equivalence  (eq? item item) checks for object equivalence. Are the two variables bound/pointing to the same object? (define l '(a b c)) (eq? l '(a b c)) » #f (define a l) (eq? a l) » #t (define a 3) (eq? a 3) » #t

32 Intro to Scheme32 Equivalence (2)  Equal? Recursively performes eq? on each entry in a list, etc.  Most types have their own equality operations (char=? #\c #\b) (= 3 3.0), etc. (define l '(a b c)) (equal? l '(a b c)) » #t

33 Intro to Scheme33 Vectors  Scheme’s answer to arrays are called vectors  Not used as much as lists  (define x (vector 1 2 3 4 5)) x » #(1 2 3 4 5)  (vector-ref x 3) » 4  (vector-set! x 2 1) x » #(1 2 1 4 5)

34 Intro to Scheme34 Conversion between data types  (char->integer #\d) » 100  (integer->char 50) » #\2  (string->list “hi!”) » (#\h #\i #\!)  (number->string 23) » “23”  (string->number “20”) » 20  (string->number “bob”) » #f  (string->number “23” 16) » 35  (symbol->string xyz) » “xyz”  (string->symbol “xyz”) » xyz All these data types are special cases of Scheme S-expressions

35 Intro to Scheme35 Problems  Draw the linked list structure that would result from each of the following expressions: (cons (cons (cons a b) c (cons d e)) f) (cons (cons (cons (cons a b) c) d) ()) ’(a b c (d e (f. g))) ’(1 2 3 (4. 5). 6)

36 Scheme E-BNF Specification

37 7.1.1 Lexical structure This section describes how individual tokens (identifiers, numbers, etc.) are formed from sequences of characters. The following sections describe how expressions and programs are formed from sequences of tokens. may occur on either side of any token, but not within a token. Tokens which require implicit termination (identifiers, numbers, characters, and dot) may be terminated by any, but not necessarily by anything else. The following five characters are reserved for future extensions to the language: [ ] { } | --> | | | | | ( | ) | #( | ' | ` |, |,@ |. --> | ( | ) | " | ; --> --> ; <all subsequent characters up to a line break> --> | --> * | --> | --> a | b | c |... | z --> ! | $ | % | & | * | / | : | < | = | > | ? | ^ | _ | ~ --> | | --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 --> + | - |. | @ --> + | - |... --> | else | => | define | unquote | unquote-splicing

38 --> quote | lambda | if | set! | begin | cond | and | or | case | let | let* | letrec | do | delay | quasiquote ` => that isn't also a > --> #t | #f --> #\ | #\ --> space | newline --> " * " --> | \" | \\ --> | | | The following rules for,,,,, and should be replicated for R = 2, 8, 10, and 16. There are no rules for,, and, which means that numbers containing decimal points or exponents must be in decimal radix. --> --> | @ | + i | - i | + i | - i | + i | - i --> | / |

39 --> |. + #* | +. * #* | + #+. #* --> + #* --> | --> | + --> e | s | f | d | l --> | + | - --> | #i | #e --> #b --> #o --> | #d --> #x --> 0 | 1 --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 --> --> | a | b | c | d | e | f 7.1.2 External representations is what the read procedure (section see section 6.6.2 Input) successfully parses. Note that any string that parses as an will also parse as a.6.6.2 Input --> | | | | --> --> |

40 --> ( *) | ( +. ) | --> --> ' | ` |, |,@ --> #( *) 7.1.3 Expressions --> | --> | | | --> ' | (quote ) --> ( *) --> --> (lambda ) --> ( *) | | ( +. ) --> * -->

41 --> (if ) --> --> | --> (set! ) --> (cond +) | (cond * (else )) | (case +) | (case * (else )) | (and *) | (or *) | (let ( *) ) | (let* ( *) ) | (letrec ( *) ) | (begin ) | (do ( *) ( ) *) | (delay ) | --> ( ) | ( ) | ( => ) --> --> (( *) ) --> ( )

42 | ( ) --> --> | --> ( *) --> (let-syntax ( *) ) | (letrec-syntax ( *) ) --> ( ) 7.1.4 Quasiquotations The following grammar for quasiquote expressions is not context-free. It is presented as a recipe for generating an infinite number of production rules. Imagine a copy of the following rules for D = 1, 2,3,.... D keeps track of the nesting depth. --> --> ` | (quasiquote ) --> | --> ( *) | ( +. ) | ' |

43 --> #( *) -->, | (unquote ) --> | -->,@ | (unquote-splicing ) In s, a can sometimes be confused with either an or a. The interpretation as an or takes precedence. 7.1.5 Transformers --> (syntax-rules ( *) *) --> ( ) --> | ( *) | ( +. ) | ( * ) | #( *) | --> | --> | ( *) | ( +. ) | #( *) | --> |

44 --> 7.1.6 Programs and definitions --> * --> | | (begin +) --> (define ) | (define ( ) ) | (begin *) --> * | *. --> (define-syntax )


Download ppt "Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely."

Similar presentations


Ads by Google