cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Similar documents
CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 61A Midterm #2 - October 5, 1998

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

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

CS61A Midterm 2 Review (v1.1)

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

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

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

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

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

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

Structure and Interpretation of Computer Programs

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


Structure and Interpretation of Computer Programs

CS450 - Structure of Higher Level Languages

Discussion 4. Data Abstraction and Sequences

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

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

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

CSE413 Midterm. Question Max Points Total 100

YOUR NAME PLEASE: *** SOLUTIONS ***

Structure and Interpretation of Computer Programs

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

Streams and Evalutation Strategies

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

Scheme Quick Reference

CS61A Notes Disc 11: Streams Streaming Along

Administrivia. Simple data types

Building a system for symbolic differentiation

University of Massachusetts Lowell

A Brief Introduction to Scheme (II)

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Scheme Quick Reference

Programming Languages

Structure and Interpretation of Computer Programs

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS 314 Principles of Programming Languages. Lecture 16

Typed Racket: Racket with Static Types

6.001: Structure and Interpretation of Computer Programs

Functional Programming - 2. Higher Order Functions

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

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

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

Project 2: Scheme Interpreter

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

The Typed Racket Guide

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

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

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

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

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

Simplifying Logical Formulas

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

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

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

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

Structure and Interpretation of Computer Programs

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

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

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

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,

CS3 Midterm 2 Summer 2008

Building a system for symbolic differentiation

CSE341 Spring 2017, Final Examination June 8, 2017

6.001 Notes: Section 8.1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CSE 341 Section Handout #6 Cheat Sheet

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Sample Final Exam Questions

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

Midterm Examination (Sample Solutions), Cmput 325

CS61c MIDTERM EXAM: 3/17/99

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

6.001 recitation 3/21/07

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CS 314 Principles of Programming Languages

Overview. CS301 Session 3. Problem set 1. Thinking recursively

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

Final Examination CSE 100 UCSD (Practice)

Documentation for LISP in BASIC

2.2 Hierarchical Data and the Closure Property

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

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

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

Introduction to Scheme

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

Structure and Interpretation of Computer Programs

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

An Explicit-Continuation Metacircular Evaluator

Functional Programming. Pure Functional Programming

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

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

CSE341 Spring 2017, Final Examination June 8, 2017

Transcription:

CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number may not be visible form where you sit, so we will help you later. This booklet contains 6 numbered pages including the cover page. Put all answers on these pages, please; don't hand in stray pieces of paper. This is an open book exam, worth 40 points, or about 13% of your total course grade. The exam contains 6 substantive questions, plus the following: Question 0 (1 point): Fill out this front page correctly and put your name and login correctly at the top of each of the following pages. When writing procedures, don't put in error checks. Assume that you will be given arguments of the correct type. Our expectation is that many of you will not complete one or two of these questions. If you find one question especially difficult, leave it for later; start with the ones you find easier. READ AND SIGN THIS: I certify that my answers to this exam are all my own work, and that I have not discussed the exam questions or answers with anyone prior to taking this exam. If I am taking this exam at other than the regularly scheduled time, I certify that I have not/ will not discuss the exam questions or answers with anyone inappropriately. 0 /1 1 /6 2 /6 3 /5 4 /6 5 /10 6 /6 total /40 1

1 Question 1 (6 points): Draw box-and-pointer diagrams that represent the results of the following expressions. (1 point each) (define x1 (list (cons 'a 'b) (list 'c 'd))) (define x2 (cons 'a (cons 'b (cons 'c 'd)))) (define x3 (list 'a 'b 'c (cons 'd '()))) (define x4 (append (list (cons 'a 'b)) (list '()))) (define x5 '((1 2) 3. 4)) (define x6 '(1 (2. 3))) Question 2 (6 points): Assume that we are using a representation of a matrix as a list of lists, namely ((1 2 3) (4 5 6) (7 8 9)) is the matrix H: 1 2 3 4 5 6 7 8 9 A. Given this definition (define (chop m)(map cdr (cdr m))), what does (chop H) return? 2

B. Given this definition (define (foo m) (if (empty? M) '() (cons m (foo (chop m))))), what does (foo H) return? C. Given the definition of accumulate on p. 116, what does this return? (accumulate * 2 (map caar (foo H))) [note for reference, here are definitions of accumulate and caar: (define (accumulate op init seq) (if (null? seq) init (op (car seq) (accumulate op init (cdr seq))))) (define (caar x) (car (car x)))] 2 Question 3 (5 points): (define (triad left label right) (lambda (m) (cond ((eq? m 't-car) left) ((eq? m 't-cbr) label) ((eq? m 't-cdr) right)))) The above constructor implements a triad [left label right] through message passing. Triads can be used to create binary trees; each node has a label, a left child, and a right child. Leaf nodes will have empty lists as their left and right children. Define selectors that retrieve the label of the current node, and the left and right child branches. (define (label node) ) (define (left node) ) (define (right node) ) 3

5 / \ 3 8 / \ / \ 1 4 7 11 Write the Scheme code to create the above tree with the triad constructor. Make sure that your selectors will work with your tree representation. 3 Question 4 (7 points): A multi-set is represented as a list of elements, some of which may be repeated. For example, (define S '(2 1 2 3 4 2)) creates a multi-set, S, with six elements having four unique values, since there are three repetitions of 2. Another way to represent multi-sets would be to explicitly keep count of repeated elements, creating cons pairs of (value. repetitions), in which case case S might be ((2. 3) (1. 1) (3. 1) (4. 1)). The ordering of the pairs does not matter. You must use the following functions to solve this problem, but you'll have to figure out what they do first. (define (count-if-member x s) (if (null? S) 0 (+ (count-if-member x (cdr s)) (if (equal? (car s) x) 1 0)))) A. In one English sentence, describe what count-if-member does. (1 pt) (define (remove x s) (cond ((null? S) '()) ((equal? (car s) x) (remove x (cdr s))) (else (cons (car s) (remove x (cdr s)))))) B. In one English sentence, describe what remove does. (1 pt) 4

Complete the program below that transforms multi-sets in the first representation to the second representation (4 pts). C. Write the program transform-multiset. If you did it the way we expect you to, you can identify each space like -A- in the skeleton function below with the function you used in your version. Look for the function name in the list below, and write the number next to the letter of the space it fits in. You may use a number more than once. If no numbered function fits, write out the name in the space below. (define (transform-multiset s) (if (-A- s) '() (-B- (-C- (-D- s) (-E- (-F- s) s)) (-G- (-H- (-I- s) (-J- s)))))) Here are your choices 1 car 4 cdr 7 union 10 map 13 equal? 2 cadr 5 cons 8 transform-multiset 11 accumulate 14 append 3 count-if-member 6 empty? 9 if 12 remove 15 list Here is where you fill in the blanks. We did one to show you how: A: B: C: D: E: F: G 8 H: I: J: 4 Question 5 (10 points): Here is a set of definitions of data objects, using message passing (define (make-inches L) (lambda (unit) (cond ((eq? unit 'inches) L) ((eq? unit 'feet) (/ L 12)) ((eq? unit 'centimeters) (* L 2.54)) (else (error "Unknown unit"))))) (define (make-feet L) (lambda (unit) (cond ((eq? unit 'inches) (* L 12)) ((eq? unit 'feet) L) ((eq? unit 'centimeters) (* L 30.48)) (else (error "Unknown unit"))))) 5

(define (make-centimeters L) (lambda (unit) (cond ((eq? unit 'inches) (/ L 2.54)) ((eq? unit 'feet) (/ L 30.48)) ((eq? unit 'centimeters) L) (else (error "Unknown unit"))))) The above functions are constructors for a length object. A. Write a selctor "length-value" that takes a length object and the units to be used for the result, and returns the numerical value of the length. (2 pts) B. Write a procedure called "area-of-rectangle-in-sq-feet" that takes two arguments: two length objects which represent the two sides of a rectangle and returns the result in square feet. (2 pts) C. A numerical package could use the same approach to multiplication: coerce all types to integers, multiply the integers and then coerce the result back to the relevant type. In one English sentence explain why this is a bad idea. (1 pt) D. In one or two English sentences, defend or attack the argument that coercion to any type (float or rational) is also bad. (2 pts) E. If we write a numerical package with three types of number: integer, rational and float explain why we do not need 6 type coercion routines. What is the minimum number we do need? (3 pts) 5 6

Question 6 (6 points): True or false? (car ''(hello)) is (quote). According to the definition of interval on page 93-94, for any interval I, the result of computing a mathematical function f(i) should represent the range of the result. If I is the interval from 0 to Π and f is the sine function in radians, and sin(0) = sin(π) = 0 then f(i) is the interval from 0 to 0. car has the closure property of section 2.2. page 97. (filter (lambda (x) (cons x x)) y) returns a list of pairs. (See page 115 for definition of filter if you don't recall it.) make-rat on page 88, and attach-tag on page 176 are the same abstraction because they are both the same as cons. In your project, frames are described by three vectors. If two frames of the same size are at the exact same position, each of the three vectors must be the same. 6 7