University of Massachusetts Lowell

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

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

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

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

A Brief Introduction to Scheme (II)

An Explicit-Continuation Metacircular Evaluator

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


Functional Programming. Pure Functional Programming

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

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

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

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

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

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

Building a system for symbolic differentiation

CS61A Midterm 2 Review (v1.1)

Structure and Interpretation of Computer Programs

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

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

Introduction to Scheme

Structure and Interpretation of Computer Programs

Project 2: Scheme Interpreter

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

Building a system for symbolic differentiation

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

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

Class Structure. Prerequisites

An Introduction to Scheme

An introduction to Scheme

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

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

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

SCHEME AND CALCULATOR 5b

An Explicit Continuation Evaluator for Scheme

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

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

4.2 Variations on a Scheme -- Lazy Evaluation

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

COP4020 Programming Assignment 1 - Spring 2011

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

Scheme as implemented by Racket

CS450 - Structure of Higher Level Languages

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CSE341 Spring 2017, Final Examination June 8, 2017

CS1 Recitation. Week 2

CSE413 Midterm. Question Max Points Total 100

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

6.001: Structure and Interpretation of Computer Programs

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

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

CSE341 Spring 2017, Final Examination June 8, 2017

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Streams and Evalutation Strategies

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

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

6.001 Notes: Section 4.1

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

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Higher order procedures procedures as types

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

Structure and Interpretation of Computer Programs

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

6.001 Notes: Section 8.1

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Administrivia. Simple data types

Functional Programming

CSc 520 Principles of Programming Languages

Essentials of Programming Languages Language

Essentials of Programming Languages Language

Problem Set 4: Streams and Lazy Evaluation

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme Quick Reference

6.001 recitation 3/21/07

Sample Final Exam Questions

Scheme Quick Reference

Fall 2017 December 4, 2017

CS61A Notes Disc 11: Streams Streaming Along

Organization of Programming Languages CS3200/5200N. Lecture 11

YOUR NAME PLEASE: *** SOLUTIONS ***

Lists in Lisp and Scheme

Discussion 4. Data Abstraction and Sequences

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

Fall 2017 December 4, Scheme. Instructions

Structure and Interpretation of Computer Programs

Functional Programming. Pure Functional Languages

How to Design Programs Languages

Berkeley Scheme s OOP

Richard Feynman, Lectures on Computation

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Chapter 1. Fundamentals of Higher Order Programming

LECTURE 16. Functional Programming

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

Documentation for LISP in BASIC

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

Transcription:

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? Assume that they are each evaluated in order in a single Scheme buffer. Write your answer below each statement. You may write procedure if a procedure [object] would be returned, or error if an error message would be returned. (This problem spans two pages.) To generate the solutions for this problem, we typed each expression to the Scheme interpreter (yes, yes, that s cheating, but we wanted to make sure we had the Right Answer). Anywhere the interpreter returned a [compound procedure...] expression, we accepted the answer procedure, and anywhere an error was triggered, we accepted error. The prefix ;Value: was not required in your answer. (define x 2) ;Value: "x --> 2" (read: x is bound to 2 ; the answer x was also accepted) x ;Value: 2 (x) ;The object 2 is not applicable ;Type D to debug error, Q to quit back to REP loop: (define (y) (* x 2)) ;Value: "y --> #[compound-procedure 2 y]" y ;Value: #[compound-procedure 2 y] (y) ;Value: 4 Quiz 1 Fall 2002

91.301 3 (define (a) (lambda (x) (+ x 1))) ;Value: "a --> #[compound-procedure 3 a]" a ;Value: #[compound-procedure 3 a] (a) ;Value: #[compound-procedure 4] (let ((x 1) (y 2) (z (+ x 4))) (+ x y z)) ;Value: 9 (define (if a b c) (+ a b c)) ;SYNTAX: define: redefinition of syntactic keyword if ;Type D to debug error, Q to quit back to REP loop: Many students erroneously thought that if could be redefined (it cannot be redefined as it is a special form). This question was not penalized, as it is linked to the question below; meaning if you got this one wrong, you only lost points on the one below. (if 2 3 4) ;Value: 3

4 91.301 Problem 2 Write a function called new-add which returns the sum of two [ integers ]. Do not use the internal functions + and -, but instead, use inc (an already defined Scheme procedure which takes one argument and returns the sum of that argument and 1) and dec (a similar procedure which returns the sum of its argument and 1). All solutions to this problem require that an iteration variable count through one argument (without loss of generality, the first, a) and as that variable counts, the other argument (the second, b) isincrementedor decremented and equal number of times. The most elegant solution we could come up with (during discussions with students directly after the exam) is based on the observation that arranging the iteration variable to count from 0 up to a, whethera is positive or negative, allows us to use the same counting operation (inc or dec) on the iteration variable as will be used on the second argument b. We capture the counting operator op inside a let, and write a small helper function to iterate through the range 0 a. (define (new-add a b) (let ((op (if (< 0 a) inc dec))) (define (new-add-helper i sum) (if (= i a) sum (new-add-helper (op i) (op sum)))) (new-add-helper 0 b))) ; determine the counting operator ; if going up, use increment ; if going down, use decrement ; done? ; yes, return the computed sum ; no, recurse (inc/dec i *and* sum) ; start running sum with b However, we accepted the following solution (and, like many of you, it was the first one we wrote): (define (new-add a b) (cond ((= a 0) b) ; return b if a has made it to zero ((< a 0) ; counting up to 0 (is a negative)? (new-add (inc a) (dec b))) ; yes, increment a, decrement b (else ; otherwise, counting down to 0 (new-add (dec a) (inc b))))) ; so, decrement a, increment b The first solution is advantageous because only one comparison on a is ever made, and it might be considered more elegant all told. The second solution, despite the fact that a is examined for being above/below 0 on every iteration, is perhaps easier to understand. Is your procedure recursive? yes Is your procedure tail-recursive? yes Is the process it generates recursive or iterative? iterative Quiz 1 Fall 2002

91.301 5 Problem 3 Assume that we have defined sum and square as follows: (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (square x) (* x x)) Determine the order of growth in time for the following functions using Θ notation. (Hint: you will only need to use one or more of the following for your answers: Θ(1), Θ(log n), Θ(n), Θ(n 2 )andθ(2 n ). All classes might not be used.) Write your answer in the blanks to the right of each function. (define (integrate a b f dx) (sum (lambda (x) (* (f x) dx)) a (lambda (x) (+ x dx)) b)) Θ(n) The astute reader would realize that the answer here really depends on the order of f, which has not been specified (bonus points for those who wrote something about that). Our answer assumes that f is a constanttime function. (define (number-of-bits-in n) (if (< n 2) 1 (+ 1 (number-of-bits-in (/ n 2))))) (define (times-5 x) (* x 5)) (define (exp a b) (cond ((< b 0) (error "Oops! b cannot be negative")) ((= b 0) 1) (else (if (odd? b) (* a (exp a (- b 1))) (square (exp a (/ b 2))))))) Θ(log n) Θ(1) Θ(log n) (define (sum-of-squares x y) (+ (square x) (square y))) Θ(1) (define (triangle-sum n) (sum (lambda (m) (sum (lambda (x) x) 1 inc m)) 1 inc n)) Θ(n 2 )

6 91.301 Problem 4 Write a procedure power-close-to that takes two non-zero positive integers (b and n) as arguments and returns the smallest power of b that is greater than n. That is, it should return the smallest integer i such that b i >n. You may use the Scheme procedure (expt b i) which raises b to the power i. Our answer is a standard tail-recursive function which increments a counter i at each iteration, checking if the desired condition has been met. If so, the counter is returned as value (not the value (expt b n)); if not, the counter is incremented and recursive execution continues. A fully-scored answer (with bonus) checked for the degenerate conditions when b is 1 (1 i = 1 for all integers i>0), and the algorithm would otherwise enter an infinite loop. (define (power-close-to b n) (define (pct i) (if (> (expt b i) n) i (pct (inc i)))) (if (= b 1) (error "Uh, sorry, there is no answer when b is 1!")) (pct 1)) To assist in evaluating answers, the following helper procedure was written. This uses the function format which has not been discussed in class (but is described in the Scheme Info entry!) to format the output in a nice, readable way. (define (check-pct) (define (helper n) (if (< n 10) (let* ((b 2) (r (power-close-to b n))) (format #t "~A^~A = ~@2A and should be greater than ~A~%" b r (expt b r) n) (helper (inc n))))) (newline) (helper 1) #t) Correctly running code should produce the following output (without checking on the degenerate case of b = 1). (check-pct) 2^1 = 2 and should be greater than 1 2^2 = 4 and should be greater than 2 2^2 = 4 and should be greater than 3 2^3 = 8 and should be greater than 4 2^3 = 8 and should be greater than 5 2^3 = 8 and should be greater than 6 2^3 = 8 and should be greater than 7 2^4 = 16 and should be greater than 8 2^4 = 16 and should be greater than 9 ;Value: #t Does your procedure generate an iterative process or a recursive process? iterative Quiz 1 Fall 2002

91.301 7 Problem 5 A local bookstore has contracted ad University to provide an inventory system for their web site. We can create a database of books using Scheme. The constructor for a single book will be called make-book and takes the name of a book and its price as parameters. (define (make-book name price) (cons name price)) Write the selectors book-name and book-price. (define (book-name b) (car b)) (define (book-price b) (cdr b)) The inventory of books will be stored in a list. The selectors for our inventory data structure are first-book and rest-books, defined as follows: (define first-book car) (define rest-books cdr) Write the constructor make-inventory. Any of (define make-inventory list) (define (make-inventory. books) books) (define make-inventory (lambda books books)) would work. Draw the box-and-pointer diagram that results from the evaluation of (define store-inventory (make-inventory (make-book sicp 60) (make-book collecting-pez 15) (make-book the-little-schemer 35)))

8 91.301 Problem 5 (continued) Write a procedure called find-book which takes the name of a book and an inventory as parameters and returns the book s data structure (name and price) if the book is in the store s inventory, and [nil] otherwise. (define (find-book name inventory) (if ((null? inventory) nil) ((eqv? (book-name (first-book inventory)) name) (first-book inventory)) (else (find-book name (rest-books inventory))))) Points were commonly lost here for breaking the abstraction barriers and for passing a book instead of the book s name. The bookstore has asked us to change our system to include a count of the number of copies of each book the store has on hand. We redefine our book constructor as follows: (define (make-book name price num-in-stock) (list name price num-in-stock)) Write the selectors book-name, book-price, andbook-stock for our new constructor. (define book-name car) (define book-price cadr) (define book-stock caddr) Will find-book need to be changed to accommodate our new representation? no (unless barriers were broken above) Quiz 1 Fall 2002

91.301 9 Problem 5 (continued) Now that we are storing the number of copies in stock, write a procedure called in-stock? that takes a book name and an inventory as the parameters, and returns #t if at least one copy of the book is in stock, or #f otherwise. If the book is not listed in the inventory at all, in-stock? should also return #f. You may want to use your find-book procedure from above. (define (in-stock? name inventory) (let ((book (find-book name inventory))) (and book (> 0 (book-stock book))))) Notice the use of and instead of if; remember that and evaluates its arguments in left-to-right order, each in turn, and only until the first evaluated argument is false (or it runs out of arguments). If all arguments evaluate non-false, then the value returned is the value of the last argument. If you used if, that would have been just fine.

10 91.301 Problem 6 Write a function add-n of one argument n that returns a procedure. The returned procedure takes one argument x and returns the sum of x and n. (define (add-n n) (lambda (x) (+ x n))) Using add-n, and without using the built-in Scheme procedure *, write mult which takes two integer arguments a and b and returns their product. There were three kinds of answers to this question. The first was along the lines of the formulation of new-add from the first quiz; the second used repeated (which worked only for positive values for the variable chosen for iteration, and thus lost points); the third was a very nice recursive formulation to deal with negative numbers. The three approaches are shown below. (define (mult a b) (let ((op-b (add-n b)) (op-i (if (< a 0) dec inc))) (define (m-helper i prod) (if (= i a) prod (m-help (op-i i) (op-b prod)))) (m-helper 0 0))) ; save the operator for b ; same for the iterator ; the helper function to iterate ; are we done? ; yes, return the answer ; no, advance counter and result ; start out at zero (define (mult a b) ((repeated (add-n a) b) 0)) ; works only for non-negative a ; add b to 0 a times (define (mult a b) (cond ((= a 0) 0) ; done? ((< a 0) ; if negative a (- (mult (- a) b))) ; then flip and continue ((add-n b) (mult (dec a) b)))) ; iterate Quiz 1 Fall 2002

91.301 11 Problem 7 Assume the following expressions have been evaluated in the order they appear. (define a (list (list q) r s)) (define b (list (list q) r s)) (define c a) (define d (cons p a)) (define e (list p (list q) r s)) Complete the table below with the result of applying the functions eq?, eqv?, andequal? to the two expressions on the left of each row. For example, the elements of the top row will represent the result from evaluating (eq? a c), (eqv? a c), and(equal? a c). Your result should be written as #t, #f or undefined [or unspecified ]. operand 1 operand 2 eq? eqv? equal? a c #t #t #t a b #f #f #t a (cdr d) #t #t #t d e #f #f #t (car a) (car e) #f #f #f (car a) (cadr e) #f #f #t (caar a) (caadr e) #t #t #t

12 91.301 Problem 8 Write occurrences, a procedure of two arguments s and tree that returns the number of times the first argument (an atom) appears in the second (a tree). You may find accumulate-tree, shown below, to be helpful. (define (accumulate-tree tree term combiner null-value) (cond ((null? tree) null-value) ((not (pair? tree)) (term tree)) (else (combiner (accumulate-tree (car tree) term combiner null-value) (accumulate-tree (cdr tree) term combiner null-value))))) Many, but not all of the students used accumulate-tree in their answer, as suggested by the hint. (define (occurrences s tree) (accumulate-tree tree (lambda (x) (if (eqv? x s) 1 0)) + 0)) Those who did not use accumulate-tree ended up writing a procedure with the same functionality. Quiz 1 Fall 2002

91.301 13 Problem 9 Draw the box and pointer diagrams for the following structures. (1) (1 2 3 4) (((1))) ((1. 2) (3. 4)) (1 (2 3) (4 (5) (6 7)))