# Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Size: px
Start display at page:

Download "Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones."

Transcription

1 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science Structure and Interpretation of Computer Programs Fall Semester, 2002 Quiz II Sample solutions Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. Part 1: (18 points) Consider the following procedure: (define (last-call proc) (let ((old *not-used*)) (lambda (arg) (let ((temporary old)) (set! old (proc arg)) temporary)))) For example, we might have: (define weird (last-call (lambda (x) (* x x)))) (weird 5) ;Value: *not-used* (weird 7) ;Value: 25 Suppose we actually evaluate the above sequence of expressions in the order shown: We want you to draw the environment diagram generated by these expressions, using diagram fragments that we provide. Attached to the exam is a tear-off sheet, with some fragments from an environment diagram. In this diagram, we have marked procedure objects as P1, P2, etc., environments as E1, E2, etc. You should EITHER complete this diagram directly on these fragments, OR you should do your own environment diagram on a separate sheet, then copy the labels for the fragments onto your diagram (we actually recommend the latter). In either case, answer the questions about the environment based on the labels used on OUR diagram!! First, for each procedure object, P1 through P4, identify, if possible, the environment pointer of the procedure (i.e. one of GE, E1,..., E8). If the appropriate environment is not shown, write not shown. Question 1. To what does the environment pointer of P1 point?

2 6.001, Fall Semester, 2002 Quiz II Sample solutions 2 GE Question 2. To what does the environment pointer of P2 point? GE Question 3. To what does the environment pointer of P3 point? E7 Question 4. To what does the environment pointer of P4 point? E6 Second, for each environment frame, indicate which environment is the enclosing environment for that frame. If the appropriate environment is not shown, write not shown. If there is no environment, write none. Question 5. What is the enclosing environment for E1? E2 Question 6. What is the enclosing environment for E2? E6 Question 7. What is the enclosing environment for E3? E6 Question 8. What is the enclosing environment for E4? E3 Question 9. What is the enclosing environment for E5? GE Question 10. What is the enclosing environment for E6? E7 Question 11. What is the enclosing environment for E7? GE For each of the following questions, choose from among these possibilities: one of the procedure objects, P1,..., P5, a number (say what number) a symbol (say what specific symbol) a list of numbers or symbols or procedure objects (which you must draw as box-and-pointer notation), an environment, E1,..., E7, GE,

3 6.001, Fall Semester, 2002 Quiz II Sample solutions 3 nothing Do this in terms of the values associated with these variables after all the expressions have been evaluated. Question 12. To what is the variable last-call in the global environment bound? P2 Question 13. To what is the variable proc in E7 bound? P1 Question 14. To what is the variable old in E6 bound? 49 Question 15. What variable is bound to the procedure object P4? Give both the name and the environment, if applicable. weird, bound in GE Question 16. What variable is bound to the procedure object P5? Give both the name and the environment, if applicable. nothing Part 2 (18 points): At the end of the quiz you will find the code for our object oriented system. The following object oriented class hierarchy covers different ways objects can hold state. Look over the code carefully before answering the questions. (define (make-a state) (lambda (msg) (case msg ((GETSTATE) (lambda (self) state)) ((ALTER) (lambda (self newstate) (set! state newstate) (ask self GETSTATE))) ((REPORT) (lambda (self) (list (ask self GETSTATE)))) (else (no-method))))) (define (make-b state) (let ((my-a (make-a state))) (lambda (msg) (case msg ((ALTER) (lambda (self newstate) (set! state newstate) (ask self GETSTATE))) ((REPORT) (lambda (self)

4 6.001, Fall Semester, 2002 Quiz II Sample solutions 4 (cons state (ask my-a REPORT)))) (else (get-method msg my-a)))))) (define (make-c state) (let ((my-a (make-a state)) (my-b (make-b state))) (lambda (msg) (case msg ((GETSTATE) (lambda (self) (ask my-a GETSTATE))) ((ALTER) (lambda (self newstate) (ask my-a ALTER newstate))) ((REPORT) (lambda (self) (delegate my-b self REPORT))) (else (get-method msg my-a)))))) Assume we make the above definitions, and then evaluate (define alex (make-a alpha)) (define bruce (make-b beta)) (define chris (make-c gamma)) What gets returned as value for each of the following expressions? (Assume that they are evaluated in this order.) You may find it helpful to draw an instance diagram or a class diagram to keep track of the state and structure of the objects created in this example. Question 17. (ask alex REPORT) (alpha) Question 18. (ask alex ALTER alef) alef Question 19. (ask alex REPORT) (alef) Question 20. (ask bruce REPORT) (beta beta) Question 21.

5 6.001, Fall Semester, 2002 Quiz II Sample solutions 5 (ask bruce ALTER betel) beta Question 22. (ask bruce REPORT) (betel beta) Question 23. (ask chris REPORT) (gamma gamma) Question 24. (ask chris ALTER gum) gum Question 25. (ask chris REPORT) (gamma gamma) Part 3 (20 points) We are going to add a new special form to our meta-circular evaluator (a copy of which is attached at the end of the quiz). The form we are going to add is an unless expression, such as (define test (1 2 3)) (unless (null? test) (display (car test)) (set! test (cdr test))) The idea is that an unless expression consists of two parts. The first sub-expression is a test clause, the remaining sub-expressions are the body. The evaluation of an unless is to first evaluate the test. If it is true, then evaluation of the entire unless is terminated, with an unspecified return value. Otherwise, the body of the unless is evaluated and the process repeats. To the meta-circular evaluator, we add the following clause ((unless? exp) (eval-unless exp env)) where

6 6.001, Fall Semester, 2002 Quiz II Sample solutions 6 (define (unless? exp) (tagged-list? exp unless)) (define (unless-test exp) (cadr exp)) (define (unless-body exp) (cddr exp)) Question 26. Write the procedure eval-unless by completing the following defintion. Be sure to use the appropriate data abstractions. (define (eval-unless exp env) (IF (NOT (M-EVAL (UNLESS-TEST EXP) ENV)) (BEGIN (EVAL-SEQUENCE (UNLESS-BODY EXP) ENV) (EVAL-UNLESS EXP ENV)))) Question 27. Suppose instead that we add the following clause to the evaluator: ((unless? exp) (m-eval (unless->if exp) env)) Complete the syntactic transformation (define (unless->if exp) (LIST IF (LIST NOT (UNLESS-TEST EXP)) (CONS BEGIN (APPEND (UNLESS-BODY EXP) (LIST EXP))))) so that this will correctly evaluate unless expressions. Part 4: (22 points) We want to construct a data abstraction called a queue. A queue consists of an ordered sequence of items, which we will represent using a list. Users of queues see a data abstraction, with a queue consisting of two components, a head and a tail. One can read the element at the head of the queue, but cannot directly access other elements in the queue. Attempting to read an empty queue is an error. One can only add new elements to a queue at the tail of the queue, and one can only remove elements from the head of the queue. For example, here is some code to create queues, and some interactions with a queue. (define (make-queue elt) (let ((temp (list elt))) (cons temp temp))) (define (head q) (car q))

7 6.001, Fall Semester, 2002 Quiz II Sample solutions 7 (define change-head! set-car!) (define (tail q) (cdr q)) (define change-tail! set-cdr!) (define (read q) (car (head q))) (define (add-queue elt q) (let ((new-part (list elt))) (cond ((null? (tail q)) INSERT-1) (else INSERT-2 INSERT-3))) q) (define (delete-queue q) (cond ((eq? (head q) (tail q)) (change-head! q #f) (change-tail! q #f)) (else INSERT-4)) q) ;; HERE IS AN EXAMPLE QUEUE (define my-queue (make-queue 1)) ;Value: ((1) 1) (read my-queue) ;Value: 1 Question 28: Draw a box and pointer diagram for the value of my-queue. my-queue > \ / \ / / / \ / 1

8 6.001, Fall Semester, 2002 Quiz II Sample solutions 8 Now, consider the following actions on a queue: (add-queue 2 my-queue) ;Value: ((1 2) 2) (read my-q) ;Value: 1 ;; NOTE: the same value is still at the head (add-queue 3 my-queue) ;Value: ((1 2 3) 3) (delete-queue my-queue) ;Value: ((2 3) 3) (read my-q) ;Value: 2 ;; NOTE: the value under the head has now changed (delete-queue my-queue) ;Value: ((3) 3) (delete-queue my-queue) ;Value: (#f) ;;NOTE: we are now left with an empty queue Question 29: Provide code for INSERT-1, which should create a queue with one element. Question 30: Provide code for INSERT-2, so that the internal representation of the elements of the queue is modified correctly. Question 31: Provide code for INSERT-3, so that the external representation of the queue is modified correctly. (define (add-queue elt q) (let ((new-part (list elt))) (cond ((null? (tail q)) (CHANGE-HEAD! Q NEW-PART) (CHANGE-TAIL! Q NEW-PART)) (else (SET-CDR! (TAIL Q) NEW-PART) (CHANGE-TAIL! Q (CDR (TAIL Q)))))) q) Question 32: Provide code for INSERT-4. (define (delete-queue q)

9 6.001, Fall Semester, 2002 Quiz II Sample solutions 9 (cond ((eq? (head q) (tail q)) (change-head! q #f) (change-tail! q #f)) (else (CHANGE-HEAD! Q (CDR (HEAD Q))))) q) Now suppose we decide to change the underlying representation of a queue, to use a list rather than a pair to represent the head and tail of the queue. (define (make-queue elt) (let ((temp (list elt))) (list temp temp))) ;; NOTE THE CHANGE HERE Question 33: What is the minimal set of definitions that must change for the system to still operate correctly? Provide the correct new definitions. (DEFINE (TAIL Q) (CADR Q)) (DEFINE (CHANGE-TAIL! Q NEW) (SET-CAR! (CDR Q) NEW)) Part 5 (22 points): Attached at the end of the quiz is a copy of the meta-circular evaluator. As it presently stands, this version of the evaluator cannot deal with procedures that use the dot notation for their arguments, such as: (define foo (lambda (x. y) (if (> x 0) (length y)))) (foo ) ;Value: 3 (foo 1) ;Value: 0 (foo 2 5) ;Value: 1 Remember that the idea behind the dot notation was to allow a procedure to be called with variable numbers of arguments. In this example, foo must be called with at least one argument, and the formal parameter x would be bound to that value. However, foo could also be called with 2 or more arguments, and the formal parameter y would then be bound to a list of the values of all the subsequent arguments. Thus in

10 6.001, Fall Semester, 2002 Quiz II Sample solutions 10 (foo ) the formal parameter x would be bound to the value 1 and the formal parameter y would be bound to the list (2 3 4). When we evaluate a lambda expression, it will create a representation for a procedure (see make-procedure in the evaluator code). In particular, when the reader converts the input expression such as (lambda (x y)...) into an internal representation for the evaluator, it provides list structure like this: --> > > _ _ _ _ \ / \ / lambda > / _ _ _ _ /_ \ / \ / X Y whereas if the reader is given input such as (lambda (x. structure like this: y)...), this is converted into list --> > > _ _ _ _ \ / \ / lambda > Y _ _ \ / X Thus when we evaluate a lambda expression whose parameter list is of the ordinary sort (e.g., (x y) or (x y z a)), the internal representation of this parameter list is an ordinary list (i.e., a sequence of cons cells whose cdr-pointers end in nil). However, when the parameter list includes a dot (such as (x. y) or (x y z. a)), the representation for this parameter list will not be an ordinary list. It will differ in one small but important detail: the cdr of the last cons cell in the sequence will point to the last parameter (rather than to nil). To change our evaluator to handle dot notation,, we just need to modify extend-environment:

11 6.001, Fall Semester, 2002 Quiz II Sample solutions 11 (define (extend-environment vars vals base-env) (let ((processed-lists (find-dot vars vals))) (cons (make-frame (car processed-lists) (cadr processed-lists)) base-env))) The find-dot procedure walks down the sequence of variable names and associated values, checking to see whether vars is an ordinary list (i.e., no dot ) or whether it is a sequence of cons cells that terminates by pointing at a variable rather than at nil (i.e., it had a dot). When we find that the cdr of the last cons cell points to a variable name rather than to nil, we want to (a) add that variable to the end of a list of variables, and (b) add the remaining values as a list, which should be added to the end of the list of all values. (Notice that we still add the variable name to a list of variables, but handle the remaining values differently.) Finally, find-dot will return a list of two lists one for the variables, and one for the values. Here is a template for this: (define (find-dot vars vals) (define (help vars-seen vars-todo vals-seen vals-todo) (cond ((null? vars-to-do) ; no more variables to examine (if (null? vals-to-do) ; if no more values either INSERT-5 ; then return collection of vars and vals (error wrong number args vars vals))) ; else report error ((null? (cdr vars-to-do)) ; we re at the last variable and ; there was no dot (if (null? vals-to-do) ; if no more values... (error wrong number args vars vals) ;...report error (if (not (null? (cdr vals-to-do))) ; else if >1 more value (error wrong number args vars vals) ; that s an error too INSERT-6))) ; otherwise, do this ((not (pair? (cdr vars-to-do))) ; sequence of vars doesn t end in nil (if (null? vals-to-do) ; if no values left... (error wrong number args vars vals) ;... report error INSERT-7)) ;... else do this (else ; otherwise collect next var and associated val, and continue (help (cons (car vars-to-do) vars-seen) (cdr vars-to-do) (cons (car vals-to-do) vals-seen) (cdr vals-to-do))))) (help () vars () vals)) Question 34: What code should be used for INSERT-5? (LIST (REVERSE VARS-SEEN) (REVERSE VALS-SEEN))

12 6.001, Fall Semester, 2002 Quiz II Sample solutions 12 Question 35: What code should be used for INSERT-6? (LIST (REVERSE (CONS (CAR VARS-TO-DO) VARS-SEEN)) (REVERSE (CONS (CAR VALS-TO-DO) VALS-SEEN))) Question 36: What code should be used for INSERT-7? (LIST (REVERSE (CONS (CDR VARS-TO-DO) (CONS (CAR VARS-TO-DO) VARS-SEEN))) (REVERSE (CONS (CDR VALS-TO-DO) (CONS (CAR VALS-TO-DO) VALS-SEEN)))

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

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

### 6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 1998, Final Exam Solutions Your

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

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

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

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

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

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

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

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

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

### Sample Final Exam Questions

91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will

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

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

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,

### Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

6.001 SICP Variations on a Scheme Scheme Evaluator A Grand Tour Techniques for language design: Interpretation: eval/appl Semantics vs. snta Sntactic transformations Building up a language... 3. 1. eval/appl

### 6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

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

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

### Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

### This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

CS 61A Final Exam May 16, 2008 Your name login: cs61a This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions. This booklet contains 18 numbered pages including

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

### MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

ps.txt Fri Apr 07 14:24:11 2017 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.945 Spring 2017 Problem Set 9 Issued: Wed. 26 April 2017 Due: Wed. 12

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

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

### Turtles All The Way Down

Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of

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

### regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

1 File: regsim.scm Register machine simulator from section 5.2 of STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS This file can be loaded into Scheme as a whole. Then you can define and simulate machines

### Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas

### Lexical vs. Dynamic Scope

Intro Lexical vs. Dynamic Scope where do I point to? Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure points to (its defining environment). This is called

### Evaluating Scheme Expressions

Evaluating Scheme Expressions How Scheme evaluates the expressions? (procedure arg 1... arg n ) Find the value of procedure Find the value of arg 1 Find the value of arg n Apply the value of procedure

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

### From Syntactic Sugar to the Syntactic Meth Lab:

From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation

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

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

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

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

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

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

### CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

### Project 5 - The Meta-Circular Evaluator

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 2005 Project 5 - The Meta-Circular

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

### CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

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

### Project 5 - The Meta-Circular Evaluator

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 2005 Project 5 - The Meta-Circular

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

### Lists in Lisp and Scheme

Lists in Lisp and Scheme a Lists in Lisp and Scheme Lists are Lisp s fundamental data structures, but there are others Arrays, characters, strings, etc. Common Lisp has moved on from being merely a LISt

### ALISP interpreter in Awk

ALISP interpreter in Awk Roger Rohrbach 1592 Union St., #94 San Francisco, CA 94123 January 3, 1989 ABSTRACT This note describes a simple interpreter for the LISP programming language, written in awk.

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

### CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers QUESTION We can also test if procedures are equal?. Consider this: > (define (square x) (* x x)) > (define (sqr x) (* x x)) > (eq?

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

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

### CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

### ;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Page 1/11 (require (lib "trace")) Allow tracing to be turned on and off (define tracing #f) Define a string to hold the error messages created during syntax checking (define error msg ""); Used for fancy

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

### 6.001, Spring Semester, 1998, Final Exam Your Name: 2 Question 1 (12 points): Each of the parts below should be treated as independent. For each part,

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 1998, Final Exam Your Name: Open

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

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

### Vectors in Scheme. Data Structures in Scheme

Data Structures in Scheme Vectors in Scheme In Scheme, lists and S-expressions are basic. Arrays can be simulated using lists, but access to elements deep in the list can be slow (since a list is a linked

### ;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the

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

### C311 Lab #3 Representation Independence: Representation Independent Interpreters

C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction

### STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? A stream is an element and a promise to evaluate the rest of the stream. Streams are a clever idea that allows one to use sequence

1 of 10 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 3 solutions 1. Box and pointer. > (let ((x (list 1 2 3))) (set-car! (cdr x) (cddr x)) x) (1 (3) 3) +-------------+ V --------- -- ------ ---------

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

### CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

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

### CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

### Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

Answers to CS61 A Final of May 23, 1997 We repeat the questions; some have typos corrected with respect to the actual exam handed out. Question 1 (5 points): Let us represent a set S of unique expressions,

### 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) (+

### Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

6.001, Fall 2007 Recitation 10 Solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall 2007

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

### CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

COMPUTER SCIENCE 320 CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination FRIDAY, MARCH 3, 2006 Problem 1. [25 pts.] A special form is an expression that is not evaluated according to the

YOUR NAME PLEASE: *** SOLUTIONS *** Computer Science 201b SAMPLE Exam 1 SOLUTIONS February 15, 2015 Closed book and closed notes. No electronic devices. Show ALL work you want graded on the test itself.

### 6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

6.034 Artificial Intelligence February 9, 2007 Recitation # 1 1 Lists and Trees (a) You are given two lists: (define x (list 1 2 3)) (define y (list 4 5 6)) What would the following evaluate to: (append

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

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

### 6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane Boning Further Variations on a Scheme Beyond Scheme

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

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

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

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

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

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

### CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Autumn 2017, Final Examination December 12, 2017 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

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

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

### Fall 2017 December 4, 2017

CS 61A Final Review Fall 2017 December 4, 2017 Instructions Form a small group. Start on the first problem. Check off with a helper or discuss your solution process with another group once everyone understands

### Simplifying Logical Formulas

Simplifying Logical Formulas Assume we have a SCHEME logical expression involving the operators and, or, not, and the truth constants #t and (). Example: (and a (not b) (or b c) (not (and c (not d))) #t

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

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

### Box-and-arrow Diagrams

Box-and-arrow Diagrams 1. Draw box-and-arrow diagrams for each of the following statements. What needs to be copied, and what can be referenced with a pointer? (define a ((squid octopus) jelly sandwich))

### CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

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

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

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