Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

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

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

Part 1: Numerical integration reloaded

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

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

Unit 10: Data Structures CS 101, Fall 2018

Sample Final Exam Questions

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

CS450 - Structure of Higher Level Languages

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

University of Massachusetts Lowell

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

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

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

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

Fall 2017 December 4, Scheme. Instructions

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

CS 216 Exam 1 Fall SOLUTION

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

A random five-digit number: a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3

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

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Structure and Interpretation of Computer Programs

Functional Programming. Pure Functional Programming

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

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

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,

1999 Midterm & Solutions

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Structure and Interpretation of Computer Programs

ECE250: Algorithms and Data Structures Midterm Review

Scheme Basics > (butfirst '(help!)) ()

Fall 2017 December 4, 2017

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

CS 314 Principles of Programming Languages. Lecture 16

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

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

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

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

Streams and Evalutation Strategies

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

8. Fundamental Data Structures

Basic Data Structures (Version 7) Name:

Discussion 4. Data Abstraction and Sequences

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

SCHEME AND CALCULATOR 5b

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

Course Review for Finals. Cpt S 223 Fall 2008

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Quiz III

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

Introduction to Functional Programming

a) For the binary tree shown below, what are the preorder, inorder and post order traversals.

Structure and Interpretation of Computer Programs

Building a system for symbolic differentiation

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

Picking up tennis balls

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Project 2: Scheme Interpreter

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

Summer 2017 Discussion 13: August 3, Introduction. 2 Creating Tables

Advanced Programming Handout 5. Purely Functional Data Structures: A Case Study in Functional Programming

Data Structure Advanced

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

User-defined Functions. Conditional Expressions in Scheme

Functional Programming - 2. Higher Order Functions

Advanced Programming Handout 6. Purely Functional Data Structures: A Case Study in Functional Programming

Problem Set 4: Streams and Lazy Evaluation

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

COMP 250 Midterm #2 March 11 th 2013

2002 Midterm Solutions

CMSC351 - Fall 2014, Homework #2

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

CSc 520 Principles of Programming Languages

Quiz II Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Department of Electrical Engineering and Computer Science

G Distributed Systems: Fall Quiz I

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

Stack and Queue. Stack:

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

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

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

A Brief Introduction to Scheme (II)

CSE413 Midterm. Question Max Points Total 100

Basic Data Structures and Heaps

INTERPRETERS AND TAIL CALLS 9

CSE 100: GRAPH ALGORITHMS

1998 Midterm and Solutions

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

UNIVERSITY REGULATIONS

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


Introduction to Data Structures

First-Class Synchronization Barriers. Franklyn Turbak Wellesley College

by the evening of Tuesday, Feb 6

6.945 Adventures in Advanced Symbolic Programming

Heap Model. specialized queue required heap (priority queue) provides at least

Transcription:

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 Recitation 10 Solutions Quiz 1 Review Quiz 1 Quiz 1 will be held next wednesday from 7:30pm-9:30pm in 50-340. Example quizzes from prior years, and recitation notes can be found at: http://people.csail.mit.edu/jastr/6001/fall07/ Queues A data structure that stores elements in order. Elements are enqueued onto the tail of the queue. Elements are tt dequeued from the head of the queue. Thus, the first element enqueued is also the first element dequeued (FIFO, first-in-first-out). The head operation is used to get the element at the head of the queue. (head (enqueue 5 (empty-queue))) ;Value: 5 (define q (enqueue 4 (enqueue 5 (enqueue 6 (empty-queue))))) (head q) ;Value: 6 (head (dequeue q)) ;Value: 5 1. Decide on an implementation for queues, then draw a box-and-pointer represention of the value of q as defined above. Have the queue be a list, with the head at the front, and the tail at the end of the list. The printed form is (6 5 4). 2. Write empty-queue. (define (empty-queue) ()) Order of growth in time?θ(1) Space? Θ(1)

6.001, Fall 2007 Recitation 10 Solutions 2 3. Write enqueue; a procedure that returns a new queue with the element added to the tail. (define (enqueue x q) (if (null? q) (list x) (cons (car q) (enqueue x (cdr q))))) Can you do this with fold-right? (define (enqueue x q) (fold-right cons (list x) q)) Order of growth in time?θ(n) Space? Θ(n) 4. Write dequeue; a procedure that returns a new queue with the head element removed. (define (dequeue q) (cdr q)) Order of growth in time?θ(1) Space? Θ(1) 5. Write head; a procedure that returns the value of the head element. (define (head q) (car q)) Order of growth in time?θ(1) Space? Θ(1) MinMax The Following questions come from a 6.001 quiz 1 circa 1995. For the questions below, assume that time is counted in the number of comparisons (< or > tests) done in the execution of the program on a sequential computer. By space we mean the additional space used by the process to keep track of the computation (We are not counting the space used by the data lists manipulated by the computation.) 1. We were presented with the following program on the quiz handout, probably written by the infamous Louis Reasoner, for computing both the minimum and the maximum of a list of numbers:

6.001, Fall 2007 Recitation 10 Solutions 3 (cons (min-list list-of-numbers) (max-list list-of-numbers))) (if (< (min-list (cdr list-of-numbers))) (min-list (cdr list-of-numbers))))) (define (max-list list-of-numbers) (if (> (max-list (cdr list-of-numbers))) (max-list (cdr list-of-numbers))))) It was noticed that this program is an enormously slow way of computing the desired result. In the worst case, just how inefficient is this program? Circle the true statements about this program in the list given below. (a) It is not worse than twice as slow as the best program for this problem. (b) Its order of growth in time is the square of the length of the input list. (c) Its order of growth in time is exponential in the length of the input list. (d) The program generates an iterative process. (e) The program takes no more than a constant amount of space to execute. (f) The program takes space of order the length of the input list to execute. 2. A staff programmer, Betir Waye, claimed that the program can be improved by the introduction of a LET form in the definition of the procedure that finds the minimum (and symmetrically the one that finds the maximum). (let ((m (min-list (cdr list-of-numbers)))) (if (< m) m)))) For the questions below, assume that time is counted in the number of comparisons (< or > tests) done in the execution of the program on a sequential computer. Just how much better is this program? Circle the true statements about this program in the list given below.

6.001, Fall 2007 Recitation 10 Solutions 4 (a) It is worse than Louis s program. (b) Its order of growth in time is linear in the length of the input list. (c) The time taken is approximately proportional to the square root of the time taken by Louis s program. (d) The program generates an iterative process. (e) The program generates a recursive process. (f) The program takes no more than a constant amount of space to execute. (g) The program takes space of order the length of the input list to execute. 3. Evan Betta suggested that we can do even better as follows: (define (accum rest min) (cond ((null? rest) min) ((< (car rest) min) (accum (cdr rest) (car rest))) (else (accum (cdr rest) min)))) )) For the questions below, assume that time is counted in the number of comparisons (< or > tests) done in the execution of the program on a sequential computer. Just how much better is this program? Circle the true statements about this program in the list given below. (a) It is worse than Betir s program. (b) Its order of growth in time is linear in the length of the input list. (c) The time taken is proportional to the square root of the time taken by Betir s program. (d) The program generates an iterative process. (e) The program generates a recursive process. (f) The program takes no more than a constant amount of space to execute. (g) The program takes space of order the length of the input list to execute. 4. After these modifications, the whole program is: (cons (min-list list-of-numbers) (max-list list-of-numbers))) (define (accum rest min) (cond ((null? rest) min) ((< (car rest) min) (accum (cdr rest) (car rest))) (else (accum (cdr rest) min))))

6.001, Fall 2007 Recitation 10 Solutions 5 )) (define (max-list list-of-numbers) (define (accum rest max) (cond ((null? rest) max) ((> (car rest) max) (accum (cdr rest) (car rest))) (else (accum (cdr rest) max)))) )) The big-wheel programmers Ben Bitdiddle and Alyssa P. Hacker appeared to testify. Alyssa pointed out that because the procedures MAX-LIST and MIN-LIST are so similar, the situation fairly cries out for some abstraction. She quickly sketched a neat plan: (cons ((min-or-max-list <) <blob1>) <blob2>)) (define (min-or-max-list comparison) (define (accum rest min-or-max) <blob3> ) (lambda (list-of-numbers) ))) Unfortunately, Alyssa dropped a slice of pepperoni pizza on her notes. In the spaces provided below, please show us the details that were obliterated with cheese: <blob1>: <blob2>: <blob3>: 5. Always practical, and never to be outdone, Ben proposed the following procedure, which accumulates the minimum and the maximum simultaneously: (cons

6.001, Fall 2007 Recitation 10 Solutions 6 ) (let ((mmr (min-and-max (cdr list-of-numbers)))) (cond ((< (car mmr)) (cons (cdr mmr))) ((> (cdr mmr)) (cons (car mmr) )) (else mmr))))) Ben s procedure generates a recursive process. In the space provided below, write an equivalent procedure that generates an iterative process: 6. Alyssa had the last word with the following mysterious procedure: (define (scan rest continue) ;; continue = (lambda (min max)...) (if (null? (cdr rest)) (continue (car rest) (car rest)) (scan (cdr rest) (lambda (min-cdr-rest max-cdr-rest) (cond ((< (car rest) min-cdr-rest) (continue (car rest) max-cdr-rest)) ((> (car rest) max-cdr-rest) (continue min-cdr-rest (car rest))) (else (continue min-cdr-rest max-cdr-rest))))))) (scan list-of-numbers cons)) Circle the true statements about this program in the list given below. (a) For any procedure that takes two numbers F: (scan (list 7 3 1 9) F) = (F 1 9) (b) (scan (list 2) +) = 4 (c) (scan (list 2 3) +) = 4 (d) (scan (list 2 3 5) *) = (scan (list 3 4 5 6 7) +) (e) For any list of numbers X and for any procedure that takes two numbers F: (scan (scan X list) F) = (scan X F)