1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
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.
1 Course progression Parallels between languages, including –sequencing, selection and repetition –abstraction mechanisms behavioral abstraction (e.g.
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
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
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Functional Programming
Environments and the Contour Model
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
6.001 SICP Variations on a Scheme
The interpreter.
Introduction to Scheme
Variables, Environments and Closures
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
6.001 SICP Data abstractions
2.3 Representation Strategies for Data Types
Variables, Environments and Closures
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Variations on a Scheme
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing problems. Many ideas in design patterns have their roots in functional programming (e.g. strategy allows us to treat methods as first-class, a decorator acts like a function which maps a function to a function – think of the stream decorators in Java). With mutation (the ability to change a name-value binding in an environment) sophisticated systems can be built quite compactly.

2 Review of evaluation model Numbers evaluation to “themselves” (a character string representing a number evaluates to its base 10 numeric representation). For example: > 213 if you ask Scheme to evaluate a number 213 you get the number as a result

3 Evaluating names A name is evaluated by looking it up. Lookup starts in the current environment, and continues along the chain of statically-linked environments until either a binding is found (in which case the corresponding value is returned) or it isn’t (in which case an error occurs). For example (assuming no binding for x exists yet): > x Error: reference to undefined identifier: x > (define x 12) > x 12 >

4 Primitives Primitives, such as + and -, are ordinary names with name-value bindings established at start-up in the primitive environment, the base environment for evaluation (base in the sense that it’s static link is null). We can use + wherever we need a function. For example: > (define applyOperator (lambda (op) (op 3 4))) > (applyOperator +) 7 > (applyOperator -) > We can also rebind the names of primitives (not a good idea in general, but this shows that primitives are not treated differently from other names in the language). > (+ 3 4) name “+” refers to addition function 7 > (define + -) name “+” now refers to subtraction function > (+ 3 4)

5 lambda forms A lambda form (lambda abstraction) defines a function in Scheme. Informally, the syntax is: (lambda ( ) ) When a lambda form is evaluated a closure results (which is printed as # ). For example: > (define addOne (lambda (p) (+ p 1))) > addOne # >

6 Function applications A function application is evaluated by first evaluating each expression inside the application, then applying the function to its arguments. This is accomplished by first creating a new environment in which the function’s parameters are bound to its arguments, and then evaluating the function’s body with respect to the new environment (which is now the current environment).

7 Evaluating (addOne 4) + addOne primitive env user env - cons … p (+ p 1) p4

8 Local bindings Local bindings can be created using a let- form: (let ((a 1) (b 2)) ) Let is just syntactic sugar for a procedure application; the above is equivalent to: ((lambda (a b) ) 1 2)

9 Evaluating (define foo (let ((a 1) (b 2)) (lambda (c) (+ a b c)))) + addOne foo primitive env user env - cons … a b (lambda (c) (+ a b c)) a1 b2 (lambda (c) (+ a b c)) c (+ a b c)

10 Mutation set! allows an existing name-value binding to be changed.