A Scheme Refresher (Functional Subset) Prabhaker Mateti.

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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
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.
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.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Scheme in Scheme 1. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Functional Programming in Scheme
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.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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 Universitatea Politehnica Bucuresti Adina Magda Florea
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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.
Scheme in Scheme 1.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
Functional Programming
History of Computing – Lisp
6.001 SICP Variations on a Scheme
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
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
Env. Model Implementation
Original material by Eric Grimson
Introduction to Functional Programming in Racket
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)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lisp and Scheme I.
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

A Scheme Refresher (Functional Subset) Prabhaker Mateti

Overview of Scheme Guy Steele and Gerald Sussman, MIT, 1975 Interpreted or compiled to machine code REPL (Read, Eval, Print, Loop) Like Python, MatLab Lisp syntax + lexical scope Lists are a fundamental built-in data type Everything in prefix form: (+ a b) instead of the infix a + b Procedures represented by computable data structures CS784 2SchemeRefresher

Standards Docs for Scheme Official Standard: The IEEE standard, (R1995) standards.ieee.org/standards.ieee.org/ De Facto Std: R6RS (2007) Revised^6 Report on the Algorithmic Language Scheme Older R5RS (1998) Documents/Standards/R5RS/ Documents/Standards/R5RS/ CS784 3SchemeRefresher

“Implementations” of Scheme Many (~100 ?) that meet R5RS and R6RS. On Linux, Windows, … CS784 choice: Previously (< June 2010) known as PLT-Scheme DrRacket, … “Determine language from source” EOPL3 Many extensions CS784 4SchemeRefresher

Scheme Reading Teach Yourself Scheme in Fixnum Days, Dorai Sitaram, free download, neu.edu/home/dorai/ (For programmers.) neu.edu/home/dorai/ How to Design Programs, Felleisen, Bruce Findler, Flatt and Krishnamurthi, MIT Press, on-line (For beginners.) Scheme Wiki: community.schemewiki.org/community.schemewiki.org/ CS784 SchemeRefresher5

Examples, Discussion, … CS784 SchemeRefresher6

Examples-1 (define x 10) (define y 20) (cons x y) (cdr (cons x y)) (symbol? '123) (string->symbol "one, two") CS784 7SchemeRefresher

Examples-2 1.(define list-length 2. (lambda (lst) 3. (if (null? lst) (+ 1 (list-length (cdr lst)))))) 1.(define remove-first 2. (lambda (s los) 3. (if (null? los) 4. '() 5. (if (eqv? (car los) s) 6. (cdr los) 7. (cons (car los) (remove-first s (cdr los))))))) CS784 8SchemeRefresher

Example-3 EOPL3 p19 1.(define occurs-free? 2. (lambda (var exp) 3. (cond 4. ((symbol? exp) (eqv? var exp)) 5. ((eqv? (car exp) 'lambda) 6. (and 7. (not (eqv? var (car (cadr exp)))) 8. (occurs-free? var (caddr exp)))) 9. (else 10. (or 11. (occurs-free? var (car exp)) 12. (occurs-free? var (cadr exp))))))) CS784 9SchemeRefresher

Example-4 EOPL3 p21 1.(define subst-in-s-exp 2. (lambda (new old sexp) 3. (if (symbol? sexp) 4. (if (eqv? sexp old) new sexp) 5. (subst new old sexp)))) (subst 'a 'b '((b c) (b () d)))  '((a c) (a () d)) CS784 10SchemeRefresher

Example-5 EOPL3 p23 1.(define number-elements-from 2. (lambda (lst n) 3. (if (null? lst) '() 4. (cons 5. (list n (car lst)) 6. (number-elements-from (cdr lst) (+ n 1)))))) 7.(define number-elements 8. (lambda (lst) 9. (number-elements-from lst 0))) (number-elements '(a b c d e))  '((0 a) (1 b) (2 c) (3 d) (4 e)) CS784 11SchemeRefresher

Built-In Data Types Booleans: #t #f Numbers Characters Strings (Unicode) Bytes and Byte Strings Symbols Keywords Pairs and Lists Vectors Hash Tables Boxes (a container for a value) Void and Undefined CS784 12SchemeRefresher

Four Types of Expressions Constants: numbers, booleans. Variables: names for values. Created using the special form define Special forms have special rules for evaluation. Cannot be redefined 15 “magic words” and, begin, case, cond, define, do, if, lambda, let, let*, letrec, or, quasiquote, quote, set! a special form is not a first-class object like a procedure Combination: (operator operands) "function calls" or "procedure applications." 13 CS784 SchemeRefresher

Mantras of Scheme Every expression has a value except for errors, infinite loops, and define special form Computing the value of a combination (operator operands …) Compute all sub-expressions (in any order) Apply the value of the first to the values of the rest Applicative Order The value of a lambda expression is a procedure CS784 SchemeRefresher14

Expressions Literals 2, 22/7, 3.1, "abc", #t '(define x 12) Variables (define x 12) x Function Calls (aka Procedure Applications) ( ‹id› ‹expr›* ) CS784 15SchemeRefresher

Symbols A symbol is an atomic value. An expression that starts with ' and continues with an identifier. Distinct from identifiers that name variables in the program text. Distinct from strings (sequence of characters). Examples (symbol? 'blah)  #t (string->symbol "one, two")  |one, two| CS784 16SchemeRefresher

Pairs cons procedure: (cons 10 20) constructs a pair  (10. 20) (cons '() 'blah)  ((). blah) (cons "blah" '())  ("blah") (cons 'blah '())  (blah) car procedure: first value cdr procedure: second value (cdr (cons 10 20))  20 (pair? '(1 2))  #tpair? CS784 17SchemeRefresher

Lists A list is recursively defined: the constant null, ornull a pair whose second value is a list. Ordered sequence of elements of arbitrary types (Heterogeneous) (list? (cons alpha (cons 2 '())))  #tlist?cons (list? (cons alpha 2))  #flist?cons CS784 18SchemeRefresher

Procedure Call/Application ( ‹id› ‹expr›* ) (proc exp1 exp2 exp3...) is in other languages proc(exp1 exp2 exp3...) (equal? 6 "half dozen")equal? ((f #t 3) (string? s) 6)string? Order of evaluation of the sub-expressions is deliberately left unspecified by Scheme. cf. C is silent about it. cf. Java specifies a left to right processing. CS784 19SchemeRefresher

procedure: (eq? v 1 v 2 ) Return #t if v1 and v2 refer to the same object, #f otherwise (eq? 'yes 'yes)  #teq? (eq? "yes" "yes" )  #f (eq? (cons 1 2) (cons 1 2))  #f CS784 20SchemeRefresher

procedure: (eqv? v 1 v 2 ) Two values are eqv? if and only if they are eq?,eqv?eq? except for number and character data typesnumbercharacter Examples (eqv? (expt 2 100) (expt 2 100))  #t (eq? (expt 2 100) (expt 2 100))  #f CS784 21SchemeRefresher

procedure: (equal? v 1 v 2 ) Two values are equal? if and only if they are eqv?,equal?eqv? except for strings, byte strings, numbers, pairs, mutable pairs, vectors, hash tables, and inspectable structures. A rule of thumb is that objects are generally equal? if they print the same. For precise details, see the docs of Scheme implementation you are using. CS784 22SchemeRefresher

eq? eqv? equal? (equal? (expt 2 100) (expt 2 100))  #t (eqv? (expt 2 100) (expt 2 100))  #t (eq? (expt 2 100) (expt 2 100))  #f (equal? (make-string 3 #\z) (make-string 3 #\z))  #t (eqv? (make-string 3 #\z) (make-string 3 #\z))  #f (eq? (make-string 3 #\z) (make-string 3 #\z))  #f CS784 23SchemeRefresher

procedure: (vector v...) Returns a new mutable “array” with the slots initialized to contain the given v … in order. Index starts at 0. (vector 'a 20 "yes")  #(a 20 "yes") (define v (vector 'a 20 "yes")) (vector-ref v 2)  "yes“ (vector-length v)  3 (vector->list v)  (a 20 "yes") (list->vector '(a 20 "yes"))  #(a 20 "yes") (build-vector 5 add1)  '#( )build-vectoradd1 CS784 24SchemeRefresher

Special Forms A special form is an expression that follows special evaluation rules. Lambda Expressions Definitions Assignments Conditionals Sequencing Iteration CS784 25SchemeRefresher

(define(define id expr) binds id to the result of expr (define x 10) (define v (vector 'a 20 "yes")) CS784 26SchemeRefresher

(define (head args) body...+)define binds id, in the head, to a procedure. (define (g x) (+ x 1)) (define ((f x)) (+ x 20)) ((f 10))  30 (f 10)  # CS784 27SchemeRefresher

(define fnid (lambda (id …) …) 1.(define (factx n) 2. (if (= n 0) 1 3. (* n (facty (- n 1))))) 4.(factx 4)  24 5.(define facty 6. (lambda (n) 7. (if (= n 0) 1 8. (* n (facty (- n 1)))))) 9.(facty 4)  (equal? factx facty)  #f 11.(eqv? factx facty)  #f 12.(eq? factx facty)  #f CS784 28SchemeRefresher

Procedures are first-class data The definition of a procedure is stored as a data structure. Can pass them as arguments to other procedures. A procedure can create and return another procedure. CS784 29SchemeRefresher

define-syntax (define-syntax id expr) 1.(define-syntax foo 2. (syntax-rules () 3. ((_ a...) 4. (printf "~a\n" (list a...))))) 5.(foo )  ( ) (define-syntax (head args) body...+)define-syntax 1.(define-syntax (bar syntax-object) 2. (syntax-case syntax-object () 3. ((_ a...) 4. #'(printf "~a\n" (list a...))))) 5.(bar )  ( ) CS784 30SchemeRefresher

(define-syntax equal?? …) 1. (define-syntax equal?? 2. (syntax-rules () 3. ((_ test-exp correct-ans) 4. (let ((observed-ans test-exp)) 5. (if (not (equal? observed-ans correct-ans)) 6. (printf "~s returned ~s, should have returned ~s~%" 7. 'test-exp 8. observed-ans 9. correct-ans) 10. (printf "pmateti ~s OK~%" 11. 'test-exp)))))) CS784 31SchemeRefresher

(apply proc v... ) → anyapply Applies proc using the content of v... as the (by-position) arguments.... (apply '(3))  6 (define compose (lambda (f g) (lambda args (f (apply g args))))) ((compose sqrt *) 12 75)  30 CS784 32SchemeRefresher

(lambda formals body) A lambda expression evaluates to an anonymous procedure. (lambda (x) (+ 12 x))  # ((lambda (x) (+ 12 x)) 20)  32 ((lambda (x y) (list y x)) 1 2)  '(2 1)list ((lambda (x [y 5]) (list y x)) 1 2)  '(2 1)list CS784 33SchemeRefresher

(if test then-exp else-exp)if If test produces any value other than #f, then then-exp is the result. Otherwise, else- exp is the result. (if (> 3 2) (- 3 2) (+ 3 2))  1 (if 'we-have-no-bananas "yes" "no")  "yes" CS784 34SchemeRefresher

(cond clause 1 clause 2...) clause ::= ( test expression ) (cond [(positive? -5) (error "doesn't get here")] [(zero? -5) (error "doesn't get here, either")] [(positive? 5) 'here])  here (cond [(member 2 '(1 2 3)) => (lambda (x) (map - x))])  (-2 -3) (cond [(member 2 '(1 2 3))])  (2 3) CS784 35SchemeRefresher

(case key clause 1 clause 2...) clause ::= ( keyList expression ) (case (- 7 5) [(1 2 3) 'small] [( ) 'big])  'small (define (classify c) (case (char-general-category c)char-general-category [(ll lu lt ln lo) "letter"] [(nd nl no) "number"] [else "other"])) (classify #\A)  "letter " (classify #\1)  "number" (classify #\!)  "other" CS784 36SchemeRefresher

Local Binding: let (let ([id val-expr]...) body...+)let Evaluates the val-exprs left-to-right, creates a new location for each id, and places the values into the locations. It then evaluates the bodys, in which the ids are bound. 1.(let ([x 5]) 2. (let ([x 2] 3. [y x]) 4. (list y x)))  '(5 2) CS784 37SchemeRefresher

Local Binding: let (let proc-id ([id init-expr]...) body...+)let Defines a local procedure. Evaluates the init-exprs; these become arguments to the proc. The ids must be distinct. (let fac ([n 10]) (if (zero? n) 1 (* n (fac (sub1 n)))))  CS784 38SchemeRefresher

Local Binding: let* (let* ([id val-expr]...) body...+)let* Similar to let, but evaluates the val-exprs one by one, creating a location for each id as soon as the value is available. The ids are bound in the remaining val-exprs as well as the bodys, and the ids need not be distinct; later bindings shadow earlier bindings.let 1.(let* ([x 1] 2. [y (+ x 1)]) 3. (list y x))  (2 1) CS784 39SchemeRefresher

Local Binding: letrec (letrec ([id val-expr]...) body...+)letrec Similar to let, but the locations for all ids are created first and filled with #, and all ids are bound in all val-exprs as well as the bodys. The ids must be distinct. 1.(letrec ((a b) (b 34) (c (+ b 5))) 2. (list a b c))  (# 34 39) 3.(letrec ([is-even? (lambda (n) 4. (or (zero? n) 5. (is-odd? (sub1 n))))] 6. [is-odd? (lambda (n) 7. (and (not (zero? n)) 8. (is-even? (sub1 n))))]) 9. (is-odd? 11))  #t CS784 40SchemeRefresher

Comparison: let, let*, letrec (let/let*/letrec ((v1 e1 ) (v2 e2 )…(vn en )) body ) let no vi is created until all ei are evaluated. none of ei can refer to any vi let* e1 is evaluated; v1 created, bound to e1; e2 is evaluated; v2 created, bound to e2; …; ej can refer to earlier vi, i < j. letrec vi are created with #undefined as their value. with the above in effect, e1, …, en are evaluated l-to-r each vi is now bound to ei CS784 41SchemeRefresher