Fall 2017 December 4, 2017

Similar documents
Fall 2017 December 4, Scheme. Instructions

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

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

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

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

TAIL CALLS, ITERATORS, AND GENERATORS 11

Structure and Interpretation of Computer Programs

Problem Set 4: Streams and Lazy Evaluation

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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

User-defined Functions. Conditional Expressions in Scheme

April 2 to April 4, 2018

Functional Programming. Pure Functional Programming

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

CS450 - Structure of Higher Level Languages

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

An Explicit-Continuation Metacircular Evaluator

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

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

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

YOUR NAME PLEASE: *** SOLUTIONS ***

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

ITERATORS AND STREAMS 9

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

Structure and Interpretation of Computer Programs

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

University of Massachusetts Lowell

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

Structure and Interpretation of Computer Programs

Scheme Quick Reference

Scheme Quick Reference

Important Example: Gene Sequence Matching. Corrigiendum. Central Dogma of Modern Biology. Genetics. How Nucleotides code for Amino Acids

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

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

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

Structure and Interpretation of Computer Programs

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

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

Structure and Interpretation of Computer Programs

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

Introduction to Functional Programming

Repetition Through Recursion

Lisp Basic Example Test Questions

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

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

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

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

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

Introduction to Scheme

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

Guerrilla Section 7: Macros, SQL SQL

Structure and Interpretation of Computer Programs

Vectors in Scheme. Data Structures in Scheme

Functional Programming - 2. Higher Order Functions

Chapter 15. Functional Programming Languages

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Simplifying Logical Formulas

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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?

Structure and Interpretation of Computer Programs

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CSE413 Midterm. Question Max Points Total 100

A Brief Introduction to Scheme (II)

Functional Programming

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

Building a system for symbolic differentiation

Imperative, OO and Functional Languages A C program is

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


CS61A Midterm 2 Review (v1.1)

CSC 533: Programming Languages. Spring 2015

Building a system for symbolic differentiation

Functional programming with Common Lisp

CS 152 / SE 152 Programming Language Paradigms

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

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

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

Lecture Notes on Lisp A Brief Introduction

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

An introduction to Scheme

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

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

GENERATORS AND STREAMS

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

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

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

Structure and Interpretation of Computer Programs

CS 275 Name Final Exam Solutions December 16, 2016

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Structure and Interpretation of Computer Programs

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

ITERATORS AND GENERATORS 10

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

Project 2: Scheme Interpreter

Transcription:

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 how to solve the first problem and then repeat for the second problem... You may not move to the next problem until you check off or discuss with another group and everyone understands why the solution is what it is. You may use any course resources at your disposal: the purpose of this review session is to have everyone learning together as a group.

2 Final Review 1 Scheme 1.1 What would Scheme display? (a) > '(1 2 3) (1 2 3) (b) > '(1. (2. (3. ()))) (1 2 3) (c) > '(((1. 2). 3) 4. (5. 6)) (((1. 2). 3) 4 5. 6) (d) > (cons 1 2) (1. 2) (e) > (cons 2 '()) (2) (f) > (cons 1 (cons 2 '())) (1 2) (g) > (cons 1 (cons 2 3)) (1 2. 3) (h) > (cons (cons (car '(1 2 3)) (list 2 3 4)) (cons 2 3)) ((1 2 3 4) 2. 3) (i) > (car (cdr (car '((1 2) 3 (4 5))))) 2 (j) > (cddr '((1 2) 3 (4 5))) ((4 5))

Final Review 3 1.2 Define sixty-ones. Return the number of times that 1 follows 6 in the list. > (sixty-ones '(4 6 1 6 0 1)) 1 > (sixty-ones '(1 6 1 4 6 1 6 0 1)) 2 > (sixty-ones '(6 1 6 1 4 6 1 6 0 1)) 3 (define (sixty-ones lst) (cond ((or (null? lst) (null? (cdr lst))) 0) ((and (= 6 (car lst)) (= 1 (cadr lst))) (+ 1 (sixty-ones (cddr lst)))) (else (sixty-ones (cdr lst))))) 1.3 Identify the bug(s) in this program. > (sum-every-other '(1 2 3)) 4 > (sum-every-other '()) 0 > (sum-every-other '(1 2 3 4)) 4 > (sum-every-other '(1 2 3 4 5)) 9 (define (sum-every-other lst) (cond ((null? lst) lst) (else (+ (cdr lst) (sum-every-other (caar lst)) )))) The base case should return 0, not '(). (cdr lst) is a list, so it doesn t make sense to add it to something. Instead, use (car lst), which will give us a number. Using caar (first of the first) is incorrect because the first is a number and it doesn t make sense to get the first of a number. Instead, we should use cddr (rest of the rest) to skip forward two elements. However, the cdr could be '(), so we need to add a case to our cond to take care of this. (define (sum-every-other lst) (cond ((null? lst) 0) ((null? (cdr lst)) (car lst)) (else (+ (car lst) (sum-every-other (cddr lst)) ))))

4 Final Review 1.4 (a) Implement add-to-all. > (add-to-all 'foo '((1 2) (3 4) (5 6))) ((foo 1 2) (foo 3 4) (foo 5 6)) (define (add-to-all item lst) (if (null? lst) lst (cons (cons item (car lst)) (add-to-all item (cdr lst))))) (b) Rewrite add-to-all tail-recursively. (define (add-to-all item lst) (define (helper item lst added) (if (null? lst) added (helper item (cdr lst) (append added (list (cons item (car lst))))))) (helper item lst '())) 1.5 Define sublists. Hint: use add-to-all. > (sublists '(1 2 3)) (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) (define (sublists lst) (if (null? lst) '(()) (let ((recur (sublists (cdr lst)))) (append recur (add-to-all (car lst) recur))))) 1.6 (a) Define reverse. Hint: use append. > (reverse '(1 2 3)) (3 2 1) (define (reverse lst) (if (null? lst) lst (append (reverse (cdr lst)) (list (car lst))))) (b) Define reverse tail-recursively. Hint: use a helper function and cons. (define (reverse lst) (define (helper lst reversed) (if (null? lst) reversed (helper (cdr lst) (cons (car lst) reversed )))) (helper lst '()))

Final Review 5 2 Interpreters 2.1 Circle the number of calls to scheme_eval and scheme_apply for the code below. (a) scm> (+ 1 2) 3 scheme_eval 1 3 4 6 scheme_apply 1 2 3 4 4 scheme_eval, 1 scheme_apply. (b) scm> (if 1 (+ 2 3) (/ 1 0)) 5 scheme_eval 1 3 4 6 scheme_apply 1 2 3 4 6 scheme_eval, 1 scheme_apply. (c) scm> (or #f (and (+ 1 2) 'apple) (- 5 2)) apple scheme_eval 6 8 9 10 scheme_apply 1 2 3 4 8 scheme_eval, 1 scheme_apply. (d) scm> (define (add x y) (+ x y)) add scm> (add (- 5 3) (or 0 2)) 2 scheme_eval 12 13 14 15 scheme_apply 1 2 3 4 13 scheme_eval, 3 scheme_apply.

6 Final Review 2.2 Identify the number of calls to scheme_eval and scheme_apply. (a) scm> (define pi 3.14) pi scm> (define (hack x) (cond ((= x pi) pwned) ((< x 0) (hack pi)) (else (hack (- x 1))))) hack 3 scheme_eval, 0 scheme_apply (b) scm> (hack 3.14) pwned 9 scheme_eval, 2 scheme_apply (c) scm> ((lambda (x) (hack x)) 0) pwned 39 scheme_eval, 10 scheme_apply

Final Review 7 3 Streams 3.1 Implement merge, which takes two streams s1 and s2 whose elements are ordered. merge returns a stream that contains elements from s1 and s2 in sorted order, eliminating repetition. You may assume s0 and s1 themselves do not contain repeats. s1 and s2 may or may not be infinite streams. (define (merge s0 s1) (cond ((null? s0) s1) ((null? s1) s0) ((= (car s0) (car s1)) (cons-stream (car s0) (merge (cdr-stream s0) (cdr-stream s1)))) ((< (car s0) (car s1)) (cons-stream (car s0) (merge (cdr-stream s0) s1))) (else (cons-stream (car s1) (merge s0 (cdr-stream s1)))) ) ) 3.2 A famous problem, first raised by Richard Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2, 3, or 5. These are called regular numbers. One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2, 3, and 5. But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, we can write a function that returns an infinite stream of such numbers. Let us call the stream of numbers s and notice the following facts about it. s begins with 1. The elements of (scale-stream s 2) are also elements of s. The same is true for (scale-stream s 3) and (scale streams5). These are all of the elements of s. Now all we have to do is combine elements from these sources. function you defined previously to fill in the definition of make-s: Use the merge (define (make-s) (cons-stream 1 (merge (scale-stream (make-s) 2) (merge (scale-stream (make-s) 3) (scale-stream (make-s) 5))) ) )

8 Final Review 4 Iterators 4.1 Define a generator that yields the sequence of perfect squares. The sequence of perfect squares looks like: 1, 4, 9, 16... def perfect_squares(): i = 0 while True: yield i * i i += 1 4.2 Implement zip, which yields a series of lists, each containing the nth items of each iterable. It should stop when the smallest iterable runs out of elements. def zip(*iterables): """ >>> z = zip_generator([1, 2, 3], [4, 5, 6], [7, 8]) >>> for i in z:... print(i)... [1, 4, 7] [2, 5, 8] """ iterators = [iter(iterable) for iterable in iterables] while True: yield [next(iterator) for iterator in iterators]

Final Review 9 4.3 Implement generate_subsets that returns all subsets of the positive integers from 1 to n. Each call to this generator s next method will return a list of subsets of the set {1, 2,..., n}, where n is the number of previous calls to next. def generate_subsets(): """ >>> subsets = generate_subsets() >>> for _ in range(3):... print(next(subsets))... [[]] [[], [1]] [[], [1], [2], [1, 2]] """ subsets = [[]] n = 1 while True: yield subsets subsets = subsets + [s + [n] for s in subsets] n += 1 We start with a base list of subsets. To get the next sequence of subsets, we need two things: All current subsets will continue to be valid subsets in the future. We take all the subsets we currently have, and add the next number. These are also valid subsets.

10 Final Review 5 SQL pizzas defines the name, open, and close hours of pizzarias. meals defines typical meal times. A pizzaria is open for a meal if the meal time is within open and close. create table pizzas as select "Pizzahhh" as name, 12 as open, 15 as close union select "La Val's", 11, 22 union select "Sliver", 11, 20 union select "Cheeseboard", 16, 23 union select "Emilia's", 13, 18; create table meals as select "breakfast" as meal, 11 as time union select "lunch", 13 union select "dinner", 19 union select "snack", 22;

Final Review 11 5.1 There s nothing wrong with going to the same pizza place for meals greater than 6 hours apart, right? Create a table double with the earlier meal, the later meal, and the name of the pizza place. Only include rows that describe two meals that are more than 6 hours apart and a pizza place that is open for both of the meals. create table double as select a.meal, b.meal, name from meals as a, meals as b, pizzas where open <= a.time and a.time <= close and open <= b.time and b.time <= close and b.time > a.time + 6; > select * from double where name="sliver"; breakfast dinner Sliver 5.2 For each meal, list all the pizza options. Create a table options that has one row for every meal and three columns. The first column is the meal, the second is the total number of pizza places open for that meal, and the last column is a commaseparated list of open pizza places in alphabetical order. Assume that there is at least one pizza place open for every meal. Order the resulting rows by meal time. Hint: Define a recursive table in a with statement that includes all partial lists of options, then use the max aggregate function to pick the full list for each meal. create table options as with lists(meal, time, names, last, n) as ( select meal, time, name, name, 1 from pizzas, meals where open <= time and time <= close union select meal, time, names ", " name, name, n + 1 from lists, pizzas where open <= time and time <= close and name > last ) select meal, max(n), names from lists group by meal order by time; > select * from options where meal="dinner"; dinner 3 Cheeseboard, La Val's, Sliver