Introduction to Scheme

Slides:



Advertisements
Similar presentations
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.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
A Scheme Refresher (Functional Subset) Prabhaker Mateti.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
ISBN Chapter 15 Functional Programming Languages.
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 CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Section 15.4, 15.6 plus other materials
ML: a quasi-functional language with strong typing
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Scheme : variant of LISP
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
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Original material by Eric Grimson
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Introduction to Scheme http://docs.racket-lang.org/quick/ http://docs.racket-lang.org/teachpack/2htdp2htdp.html http://picturingprograms.com/download/chap03.pdf cs7100(Prasad) L3Scheme

Scheme Meta-language for coding interpreters Scheme = LISP + ALGOL “clean” semantics Scheme = LISP + ALGOL simple uniform syntax; symbols and lists block structure; static scoping expression : evaluated for its value statement : evaluated for its effect Dynamic type checking flexible but inefficient (rapid prototyping) good for dealing with heterogeneous data structures cs7100(Prasad) L3Scheme

Expressions Literals Variables Procedure calls Literals Variables numerals(2), strings(“abc”), boolean(#t), etc. Variables Identifier represents a variable. Variable reference denotes the value of its binding. x ref 5 cs7100(Prasad) L3Scheme

Scheme Identifiers E.g., y,x5,+,two+two,zero?, etc. (Illegal) 5x,y)2,ab c, etc. Identifiers reserved keywords variables pre-defined functions/constants ordinary functions = procedures cs7100(Prasad) L3Scheme

Procedure Call (application) (operator-expr operand-expr ...) prefix expression (proc/op arg1 arg2 arg3 ...) Order of evaluation of the sub-expressions is “explicitly” left unspecified by Scheme. cf. C is silent about it. cf. Java specifies a left to right processing. (+ x (p 2 3)) ((f 2 3) 5 6) cs7100(Prasad) L3Scheme

Special Forms Definition Conditional > (define false #f) (define <var> <expr>) > (define false #f) Conditional (if <test> <then> <else>) > (if (zero? 5) 0 #t) > (if #t 'emptyList 'never) emptyList cs7100(Prasad) L3Scheme

Data Types values, operations, canonical representation Type-checking static : compile-time : efficient dynamic : run-time : flexible numbers: +, -, *, number?, =, etc. booleans: #t, #f, boolean?, etc. strings: string?, string->list, etc. cs7100(Prasad) L3Scheme

Symbols Identifiers treated as primitive values. Primitive operations Distinct from identifiers that name variables in the program text. Distinct from strings (sequence of characters). Primitive operations quote symbol? Facilitates meta-programming cs7100(Prasad) L3Scheme

Lists Ordered sequence of elements of arbitrary type (Heterogeneous) operations car, cdr, cons, null?, ... list, append, ... cadr, caadr, caddr, … (cadar X) = (car (cdr (car X))) cs7100(Prasad) L3Scheme

Examples (cons (quote (a)) (quote (1 2))) => ((a) 1 2) (cons ’(a) ’(1 2)) (list ’(a) ’(1 2) ’((“a”))) => ((a) (1 2) ((“a”))) (append ’(a) ’(1 2) ’((“a”))) => (a 1 2 (“a”)) cs7100(Prasad) L3Scheme

Pairs: Expression, Internal Representation and Print form (cons 'a 'b) (cons 'a (cons 'b '()) ) = (a . b) b a = (a . (b . ()) ) = (a b) Dotted pair notation () a b cs7100(Prasad) L3Scheme

Equivalence : Syntactic vs Semantic (eq? (cons 3 '()) (cons 3 '())) #f (define a (cons 3 '())) (define b (cons 3 '())) (eq? a b) (define c a) (eq? a c) #t Java: == vs .equals Scheme: eq? vs eqv? vs equal? http://people.csail.mit.edu/jaffer/r5rs_8.html#SEC49 (eqv? 'a 'b) ==> #f (eqv? '() '()) ==> #t (eqv? (cons 1 2) (cons 1 2)) ==> #f (let ((p (lambda (x) x))) (eqv? p p)) ==> #t (eqv? (lambda (x) x) (lambda (y) y)) ==> unspecified (eq? '(a) '(a)) ==> unspecified (eq? (list 'a) (list 'a)) ==> #f (eq? '() '()) ==> #t (eq? car car) ==> #t (let ((n (+ 2 3))) (eq? n n)) ==> unspecified (let ((x '(a))) (eq? x x)) ==> #t (equal? (make-vector 5 'a) (make-vector 5 'a)) ==> #t (equal? (lambda (x) x) (lambda (y) y)) ==> unspecified cs7100(Prasad) L3Scheme

Equivalence : Syntactic vs Semantic (equal? (cons 3 '()) (cons 3 '())) #t (equal? (make-vector 5 'a) (make-vector 5 'a)) (equal? (lambda(x)x) (lambda(y)y)) #f  Formally unspecified Java: == vs .equals Scheme: eq? vs eqv? vs equal? http://people.csail.mit.edu/jaffer/r5rs_8.html#SEC49 (eqv? 'a 'b) ==> #f (eqv? '() '()) ==> #t (eqv? (cons 1 2) (cons 1 2)) ==> #f (let ((p (lambda (x) x))) (eqv? p p)) ==> #t (eqv? (lambda (x) x) (lambda (y) y)) ==> unspecified (eq? '(a) '(a)) ==> unspecified (eq? (list 'a) (list 'a)) ==> #f (eq? '() '()) ==> #t (eq? car car) ==> #t (let ((n (+ 2 3))) (eq? n n)) ==> unspecified (let ((x '(a))) (eq? x x)) ==> #t (equal? (make-vector 5 'a) (make-vector 5 'a)) ==> #t (equal? (lambda (x) x) (lambda (y) y)) ==> unspecified cs7100(Prasad) L3Scheme

Vectors Both records and arrays provide random access to components. However, records are heterogeneous and provide access to components via field-names, while arrays are homogeneous and provide access to components via computable index. Vectors are heterogeneous structures that provide random access to components using a computable index. cs7100(Prasad) L3Scheme

Constructors and accessors (define v (vector “1” (+ 1 2) )) #( “1” 3 ) (define v (vector “1” ’(+ 1 2) )) #( “1” (+ 1 2) ) (vector-ref v 0) “1” (vector-length v) 2 Index is 0-based. cs7100(Prasad) L3Scheme

Procedures In Scheme, procedures are first-class objects. That is, they may be (i) passed to procedures or (ii) returned from procedures or (iii) stored in a data structure. (procedure? append) #t (if (procedure? 3) car cdr) #<procedure> cs7100(Prasad) L3Scheme

(( (if (procedure? procedure?) car cdr) (cons cdr car) ) '(list append)) = ( (car (cons cdr car)) (cdr '(list append)) '(append) Function values and higher order functions; self-application cs7100(Prasad) L3Scheme

Apply-function (apply cons '( x (y z))) = (cons 'x '(y z)) = (x y z) (apply f '(a1 a2 ... an)) = (f 'a1 'a2 ... 'an) (apply <func> <list-of-args>) Another example HOF: map BENEFIT: (0) Apply as shown enables one to unify functions of different arities. OTHER REAL BENEFITS: (1) writing function application expressions when the actual function value is to be determined at run-time. (2) defining variable-arity functions. cs7100(Prasad) L3Scheme

Apply-function Apply-function is not compelling if the function is of fixed arity and is statically known with its arguments. Apply-function is indispensable for defining variable arity function or for invoking dynamically computed function. Apply-function enables us to unify functions of different arities, and is an important component of an interpreter. Another example HOF: map BENEFIT: (0) Apply as shown enables one to unify functions of different arities. OTHER REAL BENEFITS: (1) writing function application expressions when the actual function value is to be determined at run-time. (2) defining variable-arity functions. cs7100(Prasad) L3Scheme

(apply apply (list procedure? (list apply))) = (apply apply [ proc?-fn [ apply-fn ] ] ) = (apply proc?-fn [apply-fn] ) = (procedure? apply) = #t Programming with functions cs7100(Prasad) L3Scheme

Anonymous Functions (lambda <formals-list> <body-expr>) E.g., ((lambda (n) (+ n 2)) (+ 1 4) ) = 7 Evaluate actual argument expressions Bind these values to corresponding formals in formals-list Evaluate body expression (static scoping) cs7100(Prasad) L3Scheme

Variable Arity Procedures (+ 1 2 3) (append '(1 (p q)) '() '(a b)) (list 1.2 3/4 5) (lambda <formal> <body>) <formal> is bound to the list of actual argument values supplied in a call. cs7100(Prasad) L3Scheme

(define mul (lambda x (if (null? x) 1 (* (car x) (apply mul (cdr x)) ) )) ; 1 is identity w.r.t * ) ; assuming * is binary (mul 2 (+ 2 2) 5) (mul 1 2 3) -> (apply mul ’(2 3)) Without apply, mul will get a list as actual argument. cs7100(Prasad) L3Scheme

Binding constructs in Scheme define binds value to a name. l-function application binds formal parameters to actual argument values. let-constructs introduces local bindings let let* letrec Case-lambda construct enables default arguments but does not seem to be supported by R5RS. (define substring1 (case-lambda [(s) (substring1 s 0 (string-length s))] [(s start) (substring1 s start (string-length s))] [(s start end) (substring s start end)])) cs7100(Prasad) L3Scheme

let-construct ( let ( (var1 exp1) … (varn expn)) exp ) exp1 to expn are evaluated in the surrounding context. var1,…,varn are visible only in exp. (let ( (x 2) (y 7) ) y) 7 cs7100(Prasad) L3Scheme

(let ( (x y) (y 7) ) y) *error* “y” undefined (define y 5) 7 (let ( (x y) (y 7) ) x) 5 (let ( (y 7) (x y) ) x) 5 (not 7) cs7100(Prasad) L3Scheme

let* abbreviates nested-lets. (define y 5) (let ( (y 7) (x y) ) x) 5 (let ( (y 7) ) (let ( (x y) ) x) ) 7 (let* ( (y 7) (x y) ) x) let* abbreviates nested-lets. Recursive and mutually recursive functions cannot be defined using let and let*. cs7100(Prasad) L3Scheme

letrec-construct ( letrec ( (var1 exp1) … (varn expn)) exp ) var1,…,varn are visible in exp1 to expn in addition to exp. (letrec ( (x (lambda() y)) (y (lambda() x)) ) x ;Value 1: #[compound-procedure 1 x] RACKET => #<procedure:x> (letrec ( (x (lambda() y) (y (lambda() x) ) ((((((x)))))) ) ;Value 2: #[compound-procedure 2 x] (letrec ((x (lambda()y)) (y (lambda () x))) (x)) ;Value 3: #[compound-procedure 3 y] (letrec ( (x (lambda() y)) (y (lambda() x)) ) x Similar to: (define x (lambda() y) ) (define y (lambda() x) ) ((x)) #<procedure:x> > (letrec ( (x (lambda() y)) (x) #<procedure:y> > cs7100(Prasad) L3Scheme

letrec-construct (letrec ( (f (lambda(n) (if (zero? n) 1 (f (- n 1)) )) ) ) (f 5) ) 1 (letrec ( ( f (lambda () g) ) ( g 2 ) ) ( f ) 2 (letrec ( (f (lambda(n) (if (zero? n) 1 (f (- 1 n)) )) ) ) (f 5) ) INFINITE LOOP cs7100(Prasad) L3Scheme

boolean connectives (or test1 test2 … testn) (and test1 test2 … testn) or and and are not Scheme procedures. They use short circuit evaluation rather than traditional call-by-value. cs7100(Prasad) L3Scheme

Branching constructs (cond (test1 exp1) (test2 exp2) … (testn expn) (else exp) ) (case key (keylist1 exp1) (keylist2 exp2) … (keylistn expn) (else exp) ) (case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) ===> composite (case (car '(c d)) ((a) 'a) ((b) 'b)) ===> unspecified ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) ===> consonant cs7100(Prasad) L3Scheme