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

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Lisp and Scheme I. Versions of LISP LISP is an acronym for LISt Processing language Lisp is an old language with many variants – Fortran is the only older.
A Scheme Refresher (Functional Subset) Prabhaker Mateti.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Introduction to Scheme
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
Original material by Eric Grimson
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Lisp and Scheme I.
6.001 SICP Variations on a Scheme
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Lists in Lisp and Scheme
Presentation transcript:

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

Intro to Scheme19 Basic Data Types  Numbers: 3, -3, 22/7, 3+2i, #xF3A, #o721, #b110110, (= 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”

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

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

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?

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

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

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

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

Intro to Scheme27 Lists  ’( ) » ( )  ( ) 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

Intro to Scheme28 List() (list ) 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… ( )

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 ’( )  (car x) » 1  (cdr x) » ( )  (cadr x) » 2  (cddr x) » (3 4 5)

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

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

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

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

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

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)

Scheme E-BNF Specification

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

--> 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 --> | / |

--> |. + #* | +. * #* | + #+. #* --> + #* --> | --> | + --> 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 External representations is what the read procedure (section see section Input) successfully parses. Note that any string that parses as an will also parse as a Input --> | | | | --> --> |

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

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

| ( ) --> --> | --> ( *) --> (let-syntax ( *) ) | (letrec-syntax ( *) ) --> ( ) 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 ) --> | --> ( *) | ( +. ) | ' |

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

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