# SCHEME AND CALCULATOR 5b

Size: px
Start display at page:

Transcription

1 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, we will eventually write a Scheme interpreter in Project 4. This discussion will give you some more practice with Scheme and introduce you to interpreting computer programs. 0. Warmup What Would Scheme Do STk> (define a ) STk> a STk> (define b a) STk> b STk> (define c a) STk> c STk> (define (foo x) (+ x )) STk> (define (bar x) (* x (+ x ))) STk> (bar (foo 3))

2 DISCUSSION 5B: SCHEME AND CALCULATOR Page 2 Evaluating Function Calls and Special Forms. Functions Now, you might notice that Scheme does function calls differently. To call a function in Scheme, first give the symbol for the function name, then give the arguments (remember the spaces!). But just as in Python, you evaluate the operator (the leftmost expression between the parentheses) before evaluating the arguments left to right. Then, apply the operator to the evaluated arguments. So when you evaluate (+ 2), first evaluate the + symbol which is bound to a builtin addition function. Then, evaluate the primitives and 2. Finally, apply the addition function to the arguments. Some important functions you ll want to use are: +, -, *, / eq, =, >, >=, <, <=.2 Questions. What do the following return (+ ) (* 3) (= (+ 2 ) (*.5 2)).3 Special Forms However, there are certain things that look like function calls that aren t. These are called special forms and have their own rules for evaluation. You ve already seen one- define where, of course, the first argument can t be evaluated (or else it d search for unbound variables!). Another one we ll use for this class is if. An if expression looks like this: (if <CONDITION> <THEN> <ELSE>), where <CONDITION>, <THEN>, and <ELSE> are expressions. It s evaluated exactly as it is in Python. First, the <CONDITION> is evaluated. If it evaluates to #f, then <ELSE> is evaluated. Otherwise, <THEN> is evaluated. Everything that is not #f is a true expression. CS6A Summer 203: Steven Tang and Eric Tzeng, with

3 DISCUSSION 5B: SCHEME AND CALCULATOR Page 3 STk> (if this-evaluates-to-true 2) STk> (if #f (/ 0) this-is-returned) this-is-returned There are also special forms for the boolean operators which exhibit the same shortcircuiting behavior that you see in Python. The return values are either the value that lets you know the expression evaluates to a true value or #f. STk> (and 2 3) 3 STk> (or 2 3) STk> (or #t (/ 0)) #t STk> (and #f (/ 0)) #f STk> (not 3) #f STk> (not #t) #f.4 Questions STk> (if (or #t (/ 0)) (/ 0)) STk> (if (> 4 3) ( ) (+ 3 4 (* 3 2))) STk> ((if (< 4 3) + -) 4 00) 2 Lambdas, Environments, and Defining Functions Scheme has lambdas too! In fact, lambdas are more powerful in Scheme than in Python. The syntax is (lambda (<PARAMETERS>) <EXPR>). Like in Python, lambdas are function values. Likewise, in Scheme, when a lambda expression is called, a new frame is created where the symbols defined in the <PARAMETERS> section are bound to the arguments passed in. Then, <EXPR> is evaluated under this new frame. Note that <EXPR> is not evaluated until the lambda value is called. CS6A Summer 203: Steven Tang and Eric Tzeng, with

4 DISCUSSION 5B: SCHEME AND CALCULATOR Page 4 STk> (define x 3) x STk> (define y 4) y STk> ((lambda (x y) (+ x y)) 6 7) 3 Like in Python, lambda functions are also values! So you can do this to define functions: STk> (define square (lambda (x) (* x x))) square STk> (square 4) 6 Luckily Scheme has a way out- You might notice that this is a little tedious though. define: STk> (define (square x) (* x x)) square STk> (square 5) 25 When you do (define (<FUNCTIONNAME> <PARAMETERS>) <EXPR>), Scheme will automatically transform it to (define <FUNCTIONNAME> (lambda (<PARAMETERS>) <EXPR>) for you. In this way, lambdas are more foundational to Scheme than they are to Python. Unlike Python lambdas, Scheme lambdas can have more than one statement. There is also another special form based around lambda- let. The structure of let is as follows: (let ( (<SYMBOL> <EXPR>)... (<SYMBOLN> <EXPRN>) ) <BODY> ) This special form really just gets transformed to: ( (lambda (<SYMBOL>... <SYMBOLN>) <BODY>) <EXPR>... <EXPRN>) You ll notice that what let does then is bind symbols to expressions. For example, this is useful if you need to reuse a value multiple times, or if you want to make your code more readable: (define (sin x) (if (< x ) x CS6A Summer 203: Steven Tang and Eric Tzeng, with

5 DISCUSSION 5B: SCHEME AND CALCULATOR Page 5 (let ( (recursive-step (sin (/ x 3))) ) (- (* 3 recursive-step) (* 4 (expt recursive-step 3)))))) 2. Questions. Write a function that calculates factorial. (Note how you haven t been told any methods for iteration.) (define (factorial x) 2. Write a function that calculates the Nth fibonacci number (define (fib n) (if (< n 2) ) ) 3 Pairs and Lists So far, we have lambdas and a few atomic primitives. How do we create larger more complicated data structures Well, the most important data-structure from which you ll build most complex data structures out of is the pair. A pair is an abstract data type that has the constructor cons which takes two arguments, and it has two accessors car and cdr which get the first and second argument respectively. car and cdr don t stand for anything really now but if you want the history go to wiki/car_and_cdr STk> (define a (cons 2)) a STk> a (. 2) STk> (car a) STk> (cdr a) 2 CS6A Summer 203: Steven Tang and Eric Tzeng, with

6 DISCUSSION 5B: SCHEME AND CALCULATOR Page 6 Note that when a pair is printed, the car and cdr element are separated by a period. A common data structure that you build out of pairs is the list. A list is either the empty list whose literal is (), also known as nil, another primitive, or it s a cons pair where the cdr is a list. (Note the similarity to Rlists!) STk> () () STk> nil () STk> (cons (cons 2 nil)) ( 2) STk> (cons (cons 2 (cons 3 nil))) ( 2 3) Note that there are no dots here. That is because when a dot is followed by the left parenthesis, the dot and the left parenthesis are deleted; when a left parenthesis is deleted, its matching right parenthesis is deleted also. You can check if a list is nil by using the null function. A shorthand for writing out a list is: STk> ( 2 3) ( 2 3) STk> (define (square x) (* x x)) (define (square x) (* x x)) You might notice that the return value of the second expression looks a lot like Scheme code. That s because Scheme code is made up of lists. When you use the single quote, you re telling Scheme not to evaluate the list, but instead keep it as just a list. This is one of the reasons why Scheme is so cool it can be defined within itself! 3. Questions. Define map where the first argument is a function and the second a list. This should work like Python s map. (define (map fn lst) ) CS6A Summer 203: Steven Tang and Eric Tzeng, with

7 DISCUSSION 5B: SCHEME AND CALCULATOR Page 7 2. Define reduce where the first argument is a function that takes two arguments, the second a default value and the third a list. This should work like Python s reduce. (define (reduce fn s lst) ) 4 Calculator In lecture, you saw how to implement the Calculator language using regular Python. The Calculator is a Scheme-like language that can handle the four basic arithmetic operations. These operations can be nested and can take varying numbers of arguments. Our goal now is to prepare for Project 4 by understanding the pieces of the Calculator interpreter. 4. Representing Expressions There are two kinds of expressions. A call expression is a Scheme list where the first element is the operator and each of the remaining elements is an operand. A primitive expression is an operator symbol or number. When we type a line at the Calculator prompt and hit enter, we ve just sent an expression to the interpreter. To represent Scheme lists in Python, we ll be using Pair objects. Pairs are just like the Rlists you ve come to know and love! 4.2 Questions. Translate the following Python representation of Calculator expressions into the proper Scheme representation: >>> Pair( +, Pair(, Pair(2, Pair(3, Pair(4, nil))))) ( ) >>> Pair( +, Pair(, Pair(Pair( *, Pair(2, Pair(3, nil))), nil))) >>> Pair( *, Pair(Pair( *, Pair(, Pair(2, nil))), Pair(3, nil))) 2. Translate the following Calculator expression into calls to the Pair constructor: > (+ 2 (- 3 4)) CS6A Summer 203: Steven Tang and Eric Tzeng, with

8 DISCUSSION 5B: SCHEME AND CALCULATOR Page Evaluating and Applying Evaluation discovers the form of an expression and executes the corresponding evaluation rule. Primitive expressions are evaluated directly. Call expressions are evaluated recursively as we ve seen before. First, evaluate the operator, then the operands left to right. Then, collect their values as a list of arguments and apply the operator to those arguments. If you refer to the minicalc.py file, you can see that all we ve done in the calc eval function is follow the rules of evaluation that were just outlined! If the expression is primitive (not a Pair), simply return it. Otherwise, evaluate the operands and apply the operator to the evaluated operands. We apply the operator with the calc apply function, which is a dispatch function that will dispatch on the operator name. Depending on what the operator is, we can match it to a corresponding Python call. Each conditional clause above handles the application of one operator. One way to remember the two functions: calc eval deals with expressions, whereas calc apply deals with values. 4.4 Questions. Suppose we typed each of the following expressions into the Calculator interpreter. How many calls to calc eval would they each generate How about calc apply > ( ) > (+ 2 (* 4 (- 6 8))) 2. We also want to be able to perform division, as in (/ 4 2). Supplement the existing code to handle this. If division by 0 is attempted, you should raise a ZeroDivision- Error. (Hint: a helper function that does something like Python s in operator for a Scheme list may be helpful here!) def scheme_in(elem, scheme_lst): CS6A Summer 203: Steven Tang and Eric Tzeng, with

9 DISCUSSION 5B: SCHEME AND CALCULATOR Page 9 def calc_apply(op, args):... if op == / : 3. Alyssa P. Hacker and Ben Bitdiddle are also tasked with implementing the and operator, as in (and (= 2) (< 3 4)). Ben says this is easy: they just have to follow the same process as in implementing * and /. Alyssa is not so sure. Who s right 4. Now that you ve had a chance to think about it, you decide to try implementing and yourself. You may assume the conditional operators (e.g. <, >, =, etc) have already been implemented for you. CS6A Summer 203: Steven Tang and Eric Tzeng, with

### 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 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,

### 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,

### 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,

### 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

### 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

### 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

### 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 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

### INTERPRETERS AND TAIL CALLS 9

INTERPRETERS AND TAIL CALLS 9 COMPUTER SCIENCE 61A April 9, 2015 We are beginning to dive into the realm of interpreting computer programs that is, writing programs that understand other programs. In order

### 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

### EXCEPTIONS, CALCULATOR 10

EXCEPTIONS, CALCULATOR 10 COMPUTER SCIENCE 61A November 5th, 2012 We are beginning to dive into the realm of interpreting computer programs - that is, writing programs that understand programs. In order

### Intro. Scheme Basics. scm> 5 5. scm>

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

### 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

### 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

### 61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

61A LECTURE 18 SCHEME Steven Tang and Eric Tzeng July 24, 2013 What s happening today? We re learning a new language! After you know one language (Python), learning your second (Scheme) is much faster

### Documentation for LISP in BASIC

Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

### MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

### INTERPRETATION AND SCHEME LISTS 12

INTERPRETATION AND SCHEME LISTS 12 COMPUTER SCIENCE 61A July 26, 2012 1 Calculator Language We are beginning to dive into the realm of interpreting computer programs. To do so, we will examine a few new

### CS 61A Discussion 8: Scheme. March 23, 2017

CS 61A Discussion 8: Scheme March 23, 2017 Announcements Ants is due today. Finish it! Also turn it in! HW 6 is due tomorrow. Finish it! Also turn it in! HW party today from 6:30-8:30p in 247 Cory. Midterm

### INTERPRETATION AND SCHEME LISTS 12

INTERPRETATION AND SCHEME LISTS 12 COMPUTER SCIENCE 61A July 26, 2012 1 Calculator Language We are beginning to dive into the realm of interpreting computer programs. To do so, we will examine a few new

### 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

### CALCULATOR Calculator COMPUTER SCIENCE 61A. July 24, 2014

CALCULATOR 10 COMPUTER SCIENCE 61A July 24, 2014 Remember homework 6? Let s take a second look at the Calculator language, a subset of a language we ll be learning later called Scheme. In today s discussion,

### (scheme-1) has lambda but NOT define

CS61A Lecture 12 2011-0-11 Colleen Lewis (calc) review (scheme-1) has lambda but NOT define Remember calc-apply? STk> (calc-apply '+ '(1 2 )) 6 STk> (calc-apply '* '(2 4 )) 24 STk> (calc-apply '/ '(10

### Lecture #24: Programming Languages and Programs

Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware

### 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,

### 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 Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise If you re not already crazy about Scheme (and I m sure you are), then here s something to get

### 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

### 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

### 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

### 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

### 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

### 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 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

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

### Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

### An introduction to Scheme

An introduction to Scheme 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

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is

### UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division CS61A Kurt Meinz Structure and Interpretation of Computer Programs Summer 2002

### 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,

### 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

### 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

### CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the

### Project 2: Scheme Interpreter

Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

### 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,

### Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

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:

### 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

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

### 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

### 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

### Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

### 6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

### 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

### 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:

### Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

### MetacircularScheme! (a variant of lisp)

MetacircularScheme! (a variant of lisp) Lisp = Beauty Passed on through ages Basic Expressions (function arg1 arg2 ) To evaluate an expression, apply the functionto the arguments. (+ 1 2)? => 3 (sqrt4)?

### A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

### 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

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 3: Scheme Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

### Discussion 11. Streams

Discussion 11 Streams A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the books, so I will not dwell

### Scheme as implemented by Racket

Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

### 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.

### 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

### 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

### 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

### Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

### CSE 341 Section Handout #6 Cheat Sheet

Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5

### CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

### STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? 2. How does memoization work? 3. Is a cons-stream a special form? Practice with Streams 1. Define a procedure (ones) that, when run

### 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

### 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

### ENVIRONMENT MODEL: FUNCTIONS, DATA 18

ENVIRONMENT MODEL: FUNCTIONS, DATA 18 COMPUTER SCIENCE 61A Jon Kotker and Tom Magrino July 18, 2012 1 Motivation Yesterday, we introduced the environment model of computation as an alternative to the earlier

### Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

### 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

### Discussion 12 The MCE (solutions)

Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of

### Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the

### DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

DEBUGGING TIPS COMPUTER SCIENCE 61A 1 Introduction Every time a function is called, Python creates what is called a stack frame for that specific function to hold local variables and other information.

### Structure and Interpretation of Computer Programs

CS 6A Spring 203 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,

### CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion Assorted Scheme Basics 1. The ( is the most important character in Scheme. If you have coded in other languages such as C or Java,

### A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

### Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

### Functional abstraction

Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

### 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

### 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

### 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

### Programming Languages

Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

### 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,

### RECURSION AND LINKED LISTS 3

RECURSION AND LINKED LISTS 3 COMPUTER SCIENCE 61A July 1, 2014 1 Termination and Warmup A function is recursive if executing a call to the function requires another call to the same function, called a

### 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).

### Discussion 4. Data Abstraction and Sequences

Discussion 4 Data Abstraction and Sequences Data Abstraction: The idea of data abstraction is to conceal the representation of some data and to instead reveal a standard interface that is more aligned

### CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

### April 2 to April 4, 2018

MORE SCHEME COMPUTER SCIENCE MENTORS 61A April 2 to April 4, 2018 1 Scheme 1. What will Scheme output? Draw box-and-pointer diagrams to help determine this. (a) (cons (cons 1 nil) (cons 2 (cons (cons 3

### Programming Project #4: A Logo Interpreter Summer 2005

CS 61A Programming Project #4: A Logo Interpreter Summer 2005 In Chapter 4 we study a Scheme interpreter written in Scheme, the metacircular evaluator. In this project we modify that evaluator to turn