# An introduction to Scheme

Size: px
Start display at page:

Transcription

1 An introduction to Scheme

2 Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize our ideas about processes. Thus, when we describe a language, we should pay particular attention to the means that the language provides for combining simple ideas to form more complex ideas. 2

3 Introduction (Cont.) Every poserful language has three mechanisms for accomplishing this: primitive expressions, which represent the simplest entities with which the language is concerned. means of combination., by which compound expressions are built from simpler ones, means of abstraction, by which compound objects can be named and manipulated as units. In programming, we deal with two kinds of objects: procedures and data. Thus, any powerful programming language should be able to describe primitive data and primitive procedures and should have methods fo combining and abstracting procedures and data. 3

4 Expressions In Scheme, expressions representing numbers may be combined with an expression representing a primitive procedure (such as + or *), to form a compound expression that represents the application of the procedure to those numbers. Examples: ==> ( ) 486 ==> ( ) 666 ==> (* 5 99) 495 ==> (/ 10 5) 2 ==> (/ 10 6) ==> ( )

5 Expressions (Cont.) Expressions, such as these, formed by delimiting a list of expressions within parenthesis, are called combinations. (operator operand operand... operand) Prefix notation: place operands to the left of operators. Disadvantage: prefix notation could be confusing at first. 5

6 Expressions (Cont.) Advantages: Prefix notation can accommodate procedures that take may take an arbitrary number of arguments ==> ( ) 75 Combinations can be nested ==> (+ (* 3 5) (- 10 6)) 19 But complex expressions are difficult to read: ==> (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) 6

7 Naming A critical aspect of a programming language is the means it provides for using names to refer to computational objects. In Scheme, the operator for naming things is called define ==> (define size 2) size... ==> size 2 7

8 Naming (Cont.) ==> (* 5 size) 10 ==> (define pi ) pi ==> (define radius 10) radius ==> (* pi (* radius radius) ==> (define circumference (* 2 pi radius)) circumference ==> circumference

9 Naming (Cont.) define is the simplest means of abstraction in Scheme. It allows us to use simple names to refer to the results of compound operations, such as the circumference computed above. Complex programs are constructed by building, step-by-step, computational objects of increasing complexity. The interpreter must maintain some sort of memory that keeps track of the name-object pair. This memory is called the environment. 9

10 Evaluating Combinations The (recursive) algorithm to evaluate combinations is: Evaluate the subexpressions of the combination Apply the procedure that is the value of the leftmost subexpression (the operator) to the argument that are the values of the other subexpressions (the operands). Expressions can be seen as trees. Values in trees are percolated upward starting from the terminal nodes and then combining at higher and higher levels. The leaves of these trees are primitive expressions such as numerals, built-in operators, or other names. The value of numerals are the numbers that they name The value of built in operators are the instruction sequences that carry out the corresponding operations The values of other names are the objects associated with these names in the environment. Notice that define is as exception to the general evaluation rule 10

11 Compound procedures define can be used not only to to associate names to with values, but also to associate names with compound operations. (define (square x) (* x x)) To square something multiply it by itself The general form of a procedure definition is (define ( <name> <formal parameters> ) <body> ) 11

12 Compound Procedures (Cont.) ==> (square 21) 441 ==> (square (+ 2 5)) 49 ==> (define radius 10) radius ==> (square (square 3)) 81 12

13 Compound Procedures (Cont.) We can use square as the building block for other procedures, for example x 2 +y 2 can be expressed as (+ (square x) (square y)) And this can be a new procedure ==> (define (sum-of-squares x y) ) (+ (square x) (square y)) sum=of-squares ==> (sum-of-squares 3 4) 25 ==> (define (f a) (sum-of-squares (+ a 1) (* a 2))) f ==> (f 5)

14 Conditional Expressions and Predicates The general form of the conditional expression is (cond ( <p1> <e1> ) ( <p2> <e2> )... ( <pn> <en> ) ) The arguments are pairs of expressions (<p> <e>) called clauses. The first expressions in each pair is a predicate (an expression whose value is interpreted as true or false). In Scheme, false is represented by nil, and any other object is interpreted a true. The predicate <p1> is evaluated first. If it is false, then <p2> is evaluated. If its value is also false then <p3> is evaluated. This process continues until a predicate is found whose value is true (non-nil) in which case, the interpreter rturns the corresponding consequent expression <e>. If none of the <p>s is found to be true, the cond returns a value of false. 14

15 Conditional Expressions and Predicates (Cont.) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((<x 0) (- x)))) Same procedure using else: (define (abs x) (cond ((< x 0) -x)) (else x))) Same procedure using if: (define (abs x) (if (< x 0) (- x) x)) 15

16 Conditional Expressions and Predicates (Cont.) In addition to primitive predicates <, =, and >, there are logical composition operators such as and, or and not. For example, the condition that x be in the range 5 < x < 10: (and (> x 5) (< x 10)) We can define a predicate to test whether one number is grater than or equal to another as: (define (>= x y) (or (> x y) (= x y))) or equivalently: (define (>= x y) (not (< x y))) 16

17 Square Roots by Newton s Method Newton s method solves an equation in one variable f(x)=0, by carrying out the iteration x n+ 1 = f ( x f '( x where x 0 is a first approximation (guess) to the root. To compute the square root of a number a, we need to solve the equation f(x) = x 2 -a = 0 x n+ 1 = x n x n n 2 xn a 2x = x n n n ) ) x n a / x 2 n 17

18 (define (sqrt-iter xn a) (if (good-enough? xn a) xn (define (improve xn a) (sqrt-iter (improve xn a) a))) (average xn (/ a xn))) (define (average x y) (/ (+ x y) 2)) (define (good-enough? xn a) (< abs (- (square xn) x)).001)) (define (sqrt a) (sqrt-iter 1 a)) 18

19 It is better to encapsulate auxiliary procedures inside sqrt (define (sqrt a) (define (sqrt-iter xn a) (if (good-enough? xn a) xn (define (improve xn a) (average xn (/ a xn))) (define (average x y) (/ (+ x y) 2)) (define (good-enough? xn a) (sqrt-iter (improve xn a) a))) (< abs (- (square xn) x)).001)) (sqrt-iter 1 a)) 19

20 Because of lexical scoping, we don t need that many reference to a define (sqrt a) (define (sqrt-iter x) (if (good-enough? xn) xn (sqrt-iter (improve xn)))) (define (improve xn) (average xn (/ a xn))) (define (average x y) (/ (+ x y) 2)) (define (good-enough? xn a) (< abs (- (square xn) a)).001)) (sqrt-iter 1)) 20

21 Procedures and the Processes They Generate (Cont.) A simple recursive procedure to compute the factorial of anumber (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) Can be transformed into iterative form (define (factorial n) (define (iter product counter) (iter 1 1) (if (> counter n) product (iter (* counter product)(+ counter 1)))) 21

22 Procedures and the Processes They Generate (Cont.) A simple recursive procedure to compute Fibonacci numbers (define (fib n) (cond ((= n 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) Can be transformed into iterative form as follows (define (fib n) (define (iter a b count) (iter 1 0 n)) (if (= count 0) b (iter (+ a b) a (- count 1)))) 22

23 Procedures and the Processes They Generate (Cont.) How many different ways can we make change of \$1.00, given half-dollars, quarters, dimes, nickels, and pennies? Number of ways to change amount a using n kinds of coins = Number of ways to change amount a using all but the first kind of coin + Number of ways to change amount a-d using all n kinds of coins, where d is the denomination of the first kind of coin. 23

24 Procedures and the Processes They Generate (Cont.) (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins) (cc amount (- kinds-of-coins 1)))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) (= kinds-of-coins 2) 5) (= kinds-of-coins 3) 10) (= kinds-of-coins 4) 25) (= kinds-of-coins 5) 50))) 24

25 The substitution model for procedure application Consider (define (f a) (sum-of-squares (+ a 1) (* a 2))) To evaluate (f 5) we begin by replacing the formal parameter a by 5: (sum-of-squares (+ 5 1) (* 5 2)) Now, (+ 5 1) produces 6 and (* 5 2) produces 10, so, we must apply the procedure sum-of-squares to 6 and 10. By applying substitution we get: (+ (square 6) (square 10)). And then (+ (* 6 6) (* 10 10)) Which reduces to ( ) And finally to

26 An alternative evaluation model would first expand each procedure definition in terms of simpler and simpler procedures until it obtained an expression involving only primitive operators: (sum-of-squares (+ 5 1) (* 5 2)) (+ (square (+ 5 1)) (square (* 5 2))) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) And then reduce: (+ (* 6 6) (* 10 10)) ( ) 136 This alternative fully expand and then reduce evaluation method is known as normal-order evaluation. Occurs in macros and call by name parameter passing The evaluate the arguments and then apply method that the interpreter actually uses is called application-order evaluation. 26

27 Procedures as parameters Consider the following three procedures: (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))) (define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b)))) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (* a (+ a 2))) (pi-sum (+ a 4) b)))) 27

28 We could generate each of the procedures by by filling in the slots in the template: (define (<name> a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b)))) To achieve this goal, we define: (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) 28

29 (define (sum-cubes a b) (sum cube a 1+ b)) (define (pi-sum a b) (define (pi-term x) (/1 (* x (+ x 2)))) π (define (pi-next x) 8 (+ x 4) (sum pi-term a pi-next b)) 1 = (define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2)) add-dx b) dx)) b a f dx dx = [ f ( a + ) + f ( a + dx + ) + f ( a + 2dx dx )] dx 2 29

30 Constructing procedures using lambda (define (pi-sum a b) (sum (lambda (x) (/1 (* x (+ x 2)))) a (lambda (x) (+ x 4)) b)) 30

31 Using let to define local varaibles (define (f x y) (define a ( + 1 (* x y))) (define b (- 1 y)) (+ (* x (square a)) (* y b) (* a b))) (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y))) (- 1 y))) (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y))) (- 1 y))) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 7))) (+ (* x square a)) (* y b) (* a b)))) 31

32 The general form of let is: (let ((<var1> <expr1>) (<var2> <expr2>)... (<varn> <exprn>) <body>) This is a shorthand notation for ((lambda (<var1> <var2>... <varn>) (<body>) <expr1> <expr2>... <exprn>) 32

33 An important difference between let and define is that in a let expression the variables are bound simultaneously. For example, in an evironment where x is bound to 2, the expression ((x 3) (y (+ x 2))) (* x y)) will have the value 12, but the sequence (define x 3) (define y (+ x 2)) (* x y) will result in

34 Procedures as returned values To define a procedure that implements newton s method, we need a way to evaluate the derivative. This can be done numerically with the following procedure: (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)) We can express the derivative itself as a procedure: (define (deriv f dx) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))) ==> ((deriv cube.001) 5)

35 We can use deriv to implement Newton s method: (define (newton f guess) (if (good-enough? Guess f) guess (newton f (improve guess f)))) (define (improve guess f) (- guess (/ (f guess) ((deriv f.001) guess)))) (define good-enough? guess f) (< (abs (f guess)).001)) ==> (newton (lambda(x) (-x (cos x))) 1) 35

36 Infinite or lazy data structures (define naturals (define (next n) (cons n (delay (next (+ n 1))))))) (next 1)) (define (tail stream) (force (cdr stream)))) (car naturals) -> 1 (car (tail naturals)) -> 2 (car (tail (tail naturals))) -> 3 See section in the textbook. 36

### Chapter 1. Fundamentals of Higher Order Programming

Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

### Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Introduction to Functional Programming

Introduction to Functional Programming Xiao Jia xjia@cs.sjtu.edu.cn Summer 2013 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell,

### SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

### SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

### Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

### SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### 11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

### Principles of Programming Languages

Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

### Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns Previous lecture Basics of Scheme Expressions and associated values (or syntax and

### Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

### Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

### Notes on Higher Order Programming in Scheme. by Alexander Stepanov

by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

### CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

### An Introduction to Scheme

An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Functional Programming Languages (FPL)

Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

### 1.3. Conditional expressions To express case distinctions like

Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

### FP Foundations, Scheme

FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

### Functional Programming

Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

### Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Introduction to Functional Programming in Racket CS 550 Programming Languages Jeremy Johnson 1 Objective To introduce functional programming in racket Programs are functions and their semantics involve

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

### Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

### Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky Mathematical theory for anonymous functions» functions that have

### Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004) There are only 5 ideas in Computer Science, and by the end of this course, you ll know 3 of them --Harry Mairson

### Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

### CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

### 6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

### CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

### A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

### Streams and Evalutation Strategies

Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions - message passing ( define ( make-account balance

### Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

### SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

### Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a

### TAIL RECURSION, SCOPE, AND PROJECT 4 11

TAIL RECURSION, SCOPE, AND PROJECT 4 11 COMPUTER SCIENCE 61A Noveber 12, 2012 1 Tail Recursion Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how they relate to iteration

### Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

### Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that

### Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

### Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

### University of Massachusetts Lowell

University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

### 6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

### Functional Languages. Hwansoo Han

Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

### A Brief Introduction to Scheme (I)

A Brief Introduction to Scheme (I) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Scheme Scheme I p.1/44 Scheme: Feature Set A

### 6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

### An Explicit Continuation Evaluator for Scheme

Massachusetts Institute of Technology Course Notes 2 6.844, Spring 05: Computability Theory of and with Scheme February 17 Prof. Albert R. Meyer revised March 3, 2005, 1265 minutes An Explicit Continuation

### Common LISP-Introduction

Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally

### 61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

61A LECTURE 22 TAIL CALLS, ITERATORS Steven Tang and Eric Tzeng July 30, 2013 Announcements Homework 12 due Thursday, not Monday. Take the time to get started on the project instead! Almost done with Scheme

### 6.001 Notes: Section 4.1

6.001 Notes: Section 4.1 Slide 4.1.1 In this lecture, we are going to take a careful look at the kinds of procedures we can build. We will first go back to look very carefully at the substitution model,

### Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Deferred operations Continuations 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (define the-cons (cons 1 #f)) (set-cdr! the-cons the-cons) (define (run-in-circles l) (+

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### An Explicit-Continuation Metacircular Evaluator

Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

### Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Racket: Macros Advanced Functional Programming Jean-Noël Monette November 2013 1 Today Macros pattern-based macros Hygiene Syntax objects and general macros Examples 2 Macros (According to the Racket Guide...)

### More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

More Scheme CS 331 Quiz 1. What is (car ((2) 3 4))? (2) 2. What is (cdr ((2) (3) (4)))? ((3)(4)) 3. What is (cons 2 (2 3 4))? (2 2 3 4) 4. What is the length of the list (()()()())? 4 5. Which element

### COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

### 6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

### 6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### Imperative, OO and Functional Languages A C program is

Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,

### CS1 Recitation. Week 2

CS1 Recitation Week 2 Sum of Squares Write a function that takes an integer n n must be at least 0 Function returns the sum of the square of each value between 0 and n, inclusive Code: (define (square

### CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS 480 Lisp J. Kosecka George Mason University Lisp Slides Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic

### CS450 - Structure of Higher Level Languages

Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.

Administrivia Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp) Massachusetts Institute of Technology Project 0 was due today Reminder:

### CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

### Principles of Programming Languages

Ben-Gurion University of the Negev Faculty of Natural Science Department of Computer Science Mira Balaban Lecture Notes April 9, 2013 Many thanks to Tamar Pinhas, Azzam Maraee, Ami Hauptman, Eran Tomer,

### Macros & Streams Spring 2018 Discussion 9: April 11, Macros

CS 61A Macros & Streams Spring 2018 Discussion 9: April 11, 2018 1 Macros So far, we ve mostly explored similarities between the Python and Scheme languages. For example, the Scheme list data structure

### Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1

### Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Streams CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004 We ve already seen how evaluation order can change behavior when we program with state. Now we want to investigate how

### CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan 1 Reading SICP, page 11-19, Section 1.1.6 Little Schemer, Chapter 2 2 The Idea of Syntax Abstraction Problem. Often programming tasks are repetitive,

### Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

### Functions, Conditionals & Predicates

Functions, Conditionals & Predicates York University Department of Computer Science and Engineering 1 Overview Functions as lambda terms Defining functions Variables (bound vs. free, local vs. global)

### Chapter 15. Functional Programming Languages

Chapter 15 Functional Programming Languages Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: Lisp Introduction

### 6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Today s topics Types of objects and procedures Procedural abstractions Capturing patterns across procedures Higher Order Procedures Types (+ 5 1) ==> 15 (+ "hi 5) ;The object "hi", passed as the first

### INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

INTERPRETERS 8 COMPUTER SCIENCE 61A November 3, 2016 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that understand other programs. In

### Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

CS 6A Delayed Expressions Fall 07 Discussion 9: November 8, 07 Iterables and Iterators An iterable is any container that can be processed sequentially. Examples include lists, tuples, strings, and dictionaries.

### SML A F unctional Functional Language Language Lecture 19

SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

### Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

### CS61A Midterm 2 Review (v1.1)

Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

### Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

### Functional Programming - 2. Higher Order Functions

Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

### Lisp. Versions of LISP

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 is based on Common Lisp Scheme is one of the major

### Class Structure. Prerequisites

Class Structure Procedural abstraction and recursion 6.037 - Structure and Interpretation of Computer Programs Mike Phillips, Benjamin Barenblat, Leon Shen, Ben Vandiver, Alex Vandiver, Arthur Migdal Massachusetts

### ITERATORS AND STREAMS 9

ITERATORS AND STREAMS 9 COMPUTER SCIENCE 61A November 12, 2015 1 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only

### CSC324 Functional Programming Efficiency Issues, Parameter Lists

CSC324 Functional Programming Efficiency Issues, Parameter Lists Afsaneh Fazly 1 January 28, 2013 1 Thanks to A.Tafliovich, P.Ragde, S.McIlraith, E.Joanis, S.Stevenson, G.Penn, D.Horton 1 Example: efficiency