Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
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 by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Scheme in Scheme 1. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
LISP – Not just a Speech Impediment Jim Lowe. Brief History of LISP Initial development begins at the Dartmouth Summer Research Project in 1956 by Dr.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
CMSC 330: Organization of Programming Languages
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
CS 331 Lisp/Scheme 1 Lisp/Scheme Ruth Rutter Functional and Logic Programming Team P-Phunck.
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.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Original material by Eric Grimson
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Lisp, Then and Now.
6.001 SICP Variations on a Scheme
LISP LISt Processing.
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures

Overview Scheme is a Turing-complete programming language Scheme uses the same structures to represent both programs and data: S-expressions Scheme has a simple semantics, based upon the lambda calculus Scheme is expressive enough to write a universal machine. We will write such a machine in the guise of an interpreter for Scheme written in Scheme

History Lisp is the second-oldest programming language still in current use. – Invented by John McCarthy in 1958 – Published in Comm. ACM in April current dialects: – Common Lisp – Scheme

Scheme Scheme developed at MIT by Guy L. Steele and Gerald Jay Sussman, 1975–1980 Scheme has a “minimalist” design – Small core language + mechanisms for extension Defined in a de facto standard called the Revised n Report on the Algorithmic Language Scheme (RnRS). – The most widely implemented standard is R5RS

S-expressions S-exps have a simple grammar S  Atomic | List List  ( Items ) Items  S Items |  Atomic  Symbol | Number | String | Boolean | Procedure Symbol  symbol i.e. 'X 'yz 'w34 Number  12.5 String  "abnc" i.e. things inside double quotes Boolean  #t | #f Procedure  primitive built in code

Example S-exp 25 car “abc” (cdr y) (cons 3 5) (1 4 tom 7 8) (if (> 3 x) 22 y) (quote (car x))

Representing atomic data Atomic data: data that can’t be broken into parts 25 "abc" car + #f 25 number car symbol abc string + procedure Executable code … Data is consecutive words in memory with a tag Data can have multiple components false boolean

List data (cons 3 5) cell cons symbol 3 number 5 empty symbol List are comprised of “cons cells” and pointers

S-expressions as programs Data: "abc", 25 Function calls: “polish notation” (+ 3 7) (< (- x y) 23) (reverse x) Control structures: (if (< x 5) (g t) (+ 2 x)) Declarations: (define pi (/ ))

Programs vs. data By default, an S-exp is usually interpreted as a program. Programs are (usually) “call-by-value”: – Every element of the S-exp is evaluated – The first element is assumed to be a function, which is then called with the remaining elements as arguments (+ (+ 2 5) 2)  (+ 7 2)  9 S-Exps can also be interpreted as data by quoting

Quoting If a quote precedes an S-exp, as in '(+ 2 3) or '(4 5 6), then the S-exp following the quote is interpreted as data. A quoted S-exp evaluates to itself '(a b q) is shorthand for (quote (a b c)) > '(+ 3 4)

Function calls (h z y z) h  procedure z  value y  value z  value The procedure is then applied to all the arguments (g 2 3) (f 1)

Constructing Data Quoting '(1 3 5) Dot expressions '(3. 4) Cons (cons 3 (cons 4 '()))  '(3 4) List (list (+ 3 4) 'abc 9)  '(7 abc 9) cell 1 number 3 5 nil atom cell 3 number 4

Equality Primitive equality on atomic data –(eq? x y) are x and y the same object? Structural equality –(equal? x y) do x and y have the same structure? > (eq? 'a 'a) #t > (eq? '(1 2) '(1 2)) #f >(equal? '(1 2) '(1 2)) #t

Symbols are unique All Symbols with the same name are represented inside the machine by the same object. > (eq? 'abc 'abc) #t > (eq? "abc" (string-append "ab" "c")) #f >(eq? 'abc (string->symbol (string-append "ab" "c"))) #t

Control structures quote lambda cond define These are part of the language, not functions Conditionals – (cond ((test 1 exp 1 )(test 2 exp 2 ) … ))

Definitions (define x 25) (define (f x) (+ x 9)) (define g +)

Anonymous functions In Scheme, one can write anonymous functions (i.e., functions without a name) –(lambda (param 1 … param n ) body) Lambdas evaluate to procedures > (lambda (x y) (+ (+ x 1) y)) # > ((lambda (x y) (+ (+ x 1) y)) 5 7) 13

Local binding let and let* (define (f x) (let ((y 1) (z 3)) (+ x y z))) (define (f x) (let* ((y 1) (z (+ y 3))) (+ x y z)))t* Later bindings can see earlier ones

Lexical scoping Free variables in anonymous functions see the closest enclosing definition. (define x 25) (define y (let ((x 1)) ((lambda (z) (+ z x)) 12)))

Functions as arguments In Scheme, one can write functions that take other functions as arguments. (define (app1 f y) (cond ((symbol? y) (f y)) ((list? y) (cons (f (car y)) (cdr y))))) > (app1 symbol? 'a) #t > (app1 symbol? '(a b c)) (#t b c)

Important higher order functions map apply >(map (lambda (x) (+ x 3)) '(1 2 3)) (4 5 6) >(apply string-append '("abc" "zyz")) "abczyz"

Summary Scheme is a simple language without much syntax Programs and data have the same representation One can write programs that build and manipulate programs Functions and arguments are treated the same (f x y z)