YOUR NAME PLEASE: *** SOLUTIONS ***

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

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

A Brief Introduction to Scheme (II)

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

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

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

CS 314 Principles of Programming Languages

User-defined Functions. Conditional Expressions in Scheme


Structure and Interpretation of Computer Programs

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

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

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

CS450 - Structure of Higher Level Languages

CS 314 Principles of Programming Languages. Lecture 16

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

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

CS61A Midterm 2 Review (v1.1)

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

Adding Machine Run 2

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

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

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 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Scheme as implemented by Racket

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

Streams and Evalutation Strategies

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

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

Scheme: Strings Scheme: I/O

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Functional programming with Common Lisp

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

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

Functional Programming. Pure Functional Languages

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

Structure and Interpretation of Computer Programs

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

CS 44 Exam #2 February 14, 2001

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

Sample Final Exam Questions

Functional Programming. Pure Functional Programming

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

Functional Programming

Building a system for symbolic differentiation

Structure and Interpretation of Computer Programs

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

CS 314 Principles of Programming Languages

Fall 2017 December 4, 2017

Fall 2017 December 4, Scheme. Instructions

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Structure and Interpretation of Computer Programs

CSE413 Midterm. Question Max Points Total 100

Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

University of Massachusetts Lowell

LECTURE 16. Functional Programming

CS150 - Sample Final

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

CSc 520 Principles of Programming Languages

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

Introduction to Scheme

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

CS 314 Principles of Programming Languages

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

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

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

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

CpSc 421 Final Solutions

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

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

Scheme Quick Reference

Building a system for symbolic differentiation

(scheme-1) has lambda but NOT define

Structure and Interpretation of Computer Programs

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

Module 9: Binary trees

Discussion 4. Data Abstraction and Sequences

CS 415 Midterm Exam Fall 2003

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

Functional Programming. Pure Functional Languages

Introduction to Functional Programming

Repetition Through Recursion

CS 275 Name Final Exam Solutions December 16, 2016

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

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

Scheme: Expressions & Procedures

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

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

My dear students, Believe in yourselves. Believe in your abilities. You can DO this! -Dr. M

Transcription:

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. For problems that do not ask you to justify the answer, an answer alone is sufficient. However, if the answer is wrong and no derivation or supporting reasoning is given, there will be no partial credit. GOOD LUCK! problem points actual 1 10 2 10 3 10 4 10 5 10 6 10 total 60 1

1.(a) (5 points) Define a Racket procedure (change item1 item2 lst) that changes every top-level occurrence of item1 in lst to item2. Use only the built-in Racket special forms and procedures: define, lambda, null?, empty?, equal?, if, cond, car, first, cdr, rest, cons, and quoted constant lists like (). Do not write any auxiliary procedures for this problem. Examples: (change 3 4 (4 5 3 4 4 3 0)) => (4 5 4 4 4 4 0) (change a b (a l a s (a n d))) => (b l b s (a n d)) (change (0) x (1 (0) 1 (1) (0))) => (1 x 1 (1) x) (define (change item1 item2 lst) (cond [(null? lst) ()] [(equal? item1 (first lst)) (cons item2 (change item1 item2 (rest lst)))] [else (cons (first lst) (change item1 item2 (rest lst)))])) 2

1.(b) (5 points) Draw a tree of recursive calls, with return values, for your procedure when evaluating (change 3 8 (1 3 4 3)). (change 3 8 (1 3 4 3)) => (1 8 4 8) / \ cons 1 (change 3 8 (3 4 3)) => (8 4 8) / \ cons 8 (change 3 8 (4 3)) => (4 8) / \ cons 4 (change 3 8 (3)) => (8) / \ cons 8 (change 3 8 ()) => () 3

2. Consider the boolean function f presented by the following truth table: x y z f --------- 0 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 2. (a) (5 points) Write down a Boolean expression (using dot for AND, plus for OR and prime for NOT) to represent f using the variables x, y, z. Justify its correctness. x *y *z + x*y *z + x*y*z (dots shown as * here) via the sum-of-products algorithm. 4

2. (b) (5 points) Give a Boolean expression (using AND, OR, NOT as in part (a)) corresponding to each of the following statements involving the Boolean variables P, Q, and R. You need not justify correctness. (i) At least one of P, Q, R is 1. (P + Q + R) (ii) At most one of P, Q, R is 1. P *Q + P *R + Q *R (iii) R is 1 if P and Q are both 0, and R is 0 otherwise. (P+Q)*R + P *Q *R (iv) P, Q and R all have the same value. P*Q*R + P *Q *R (v) Exactly two of P, Q, R are 0. P*Q *R + P *Q*R + P *Q *R 5

3. Define an nb-list to be a list that is empty or has as elements numbers, booleans, or nb-lists. For example, the list (#t 1 (2 ()) (#f #f))) is an nb-list. 3. (a) (6 points) Define a recursive Racket procedure (numbers lst) that takes an nb-list lst and returns a list of all the numbers that occur in lst, in the left-to-right order in which they occur. Preserve duplicates. Use only the built-in Racket special forms and procedures: define, lambda, null?, empty?, symbol?, boolean?, number?, if, cond, car, first, cdr, rest, cons, append, and quoted constant lists like (). Do not write any auxiliary procedures for this problem. Examples: (numbers (#f 1 (2 ()) ((1)))) => (1 2 1) (numbers (#t 2 ((#f)) 26 #f (3 2))) => (2 26 3 2) (define (numbers lst) (cond [(null? lst) ()] [(number? (first lst)) (cons (first lst) (numbers (rest lst)))] [(list? (first lst)) (append (numbers (first lst)) (numbers (rest lst)))] [else (numbers (rest lst))])) 6

3.(b) (4 points) Draw a tree of recursive calls (no return values) for your procedure numbers from part 3.(a) when you evaluate (numbers ((#t 1) (4 5))). (numbers ((#t 1) (4 5))) -------------------------- / \ append (numbers (#t 1)) (numbers ((4 5))) ---------------- --------------------- / \ (numbers (1)) append (numbers (4 5)) (numbers ()) ------------- ---------------- / \ / \ cons 1 (numbers ()) cons 4 (numbers (5)) -------------- / \ cons 5 (numbers ()) 7

4.(a) (8 points) Construct a Turing machine to multiply a unary number by 2. In the initial configuration the tape has a contiguous sequence of m 1 s, and all other symbols are blank, the machine is in state q1 and the head is on the leftmost 1. In the final configuration the tape has a contiguous sequence of n 1 s, and all other symbols are blank, the machine is halted and the head is on the leftmost 1. Moreover, n = 2m. For example, for input 111 the output should be 111111. You may use additional tape symbols. You may assume m > 0. You must give a clear explanation of how your machine works -- this is worth half the points on this problem. The machine will repeatedly change the next 1 into an x and write an x at the left end of the tape, until all the 1 s are converted to x s. Then it will rewrite all the x s as 1 s and restore the head to the start of the sequence of 1 s. (q1, x, q1, x, R) q1 runs right over x s (q1, b, q3, b, L) if it finds a blank, all 1 s are converted, go to q3 (q1, 1, q2, x, L) if it finds a 1, it converts it to x, and goes to q2 (q2, x, q2, x, L) q2 runs left over x s until blank (q2, b, q1, x, R) and writes a new x there and goes back to q1 (q3, x, q3, 1, L) q3 moves left over x s, changing them to 1 s (q3, b, q4, b, R) until it finds a blank, when it goes to q4 to halt 8

4.(b) (2 points) An initial configuration for your machine is given below with the head on the leftmost of a sequence of of two 1 s, the rest of the tape blank, in state q1. Please show the next six configurations in order. Number them 2 through 7. (Note that b is the blank symbol.) 1) b 1 1 b 5) x x x b q1 q2 2) b x 1 b 6) x x x b q2 q2 3) x x 1 b 7) b x x x b q1 q2 4) x x 1 b q1 9

5. (10 points) 5.(a) Give a careful statement of the theorem from lecture about the Halting Problem for Racket programs. There is no procedure (halts? proc expr) that for any Racket procedure proc and Racket expression expr will always return #t if (proc expr) halts and #f if (proc expr) does not halt. 5.(b) Give an example of the use of the cd command in Linux, and explain briefly what it does. > cd cs201 > Changes the current directory (or folder) to the given one. 5.(c) Is the following procedure tail recursive? Why or why not? (define (our-length lst) (if (null? lst) 0 (+ 1 (our-length (cdr lst))))) No, because the value of the recursive call, (our-length (cdr lst)), has 1 added to it before it is returned as the value of (our-length lst). 10

5.(d) Draw a tree of recursive calls (no return values) for the application (rep #t 2), where the procedure rep is defined as follows. (define (rep value n) (if (= n 0) (list value) (list (rep value (- n 1)) (rep value (- n 1))))) (rep #t 2) ----------------------------------------- list (rep #t 1) (rep #t 1) ----------------- ----------------- list (rep #t 0) (rep #t 0) list (rep #t 0) (rep #t 0) 5.(e) Rewrite the procedure in 5.(d) using the special form let to avoid redundant computation. (define (rep value n) (if (= n 0) (list value) (let ((prev (rep value (- n 1)))) (list prev prev)))) 11

6. (10 points) Evaluate the following Racket expressions. (Remember to show work to permit partial credit!) Example: (cons 1 (6 5)) => (1 6 5) (a) (cons (1 2) (3 4)) => ((1 2) 3 4) (b) (cadr ((apple book) (cat) (doubt eel))) => (cat) (c) (map (lambda (x) (cons 0 x)) (() (2) (3 5))) => ((0) (0 2) (0 3 5)) (d) (list (+ 1 2) (list 1 2)) => (3 (1 2)) (e) (((lambda (x) (if (odd? x) (lambda (x) (+ x 1)) (lambda (x) (- x 1)))) 3) 12) => 13 12