CS61A Midterm 2 Review (v1.1)

Similar documents

(scheme-1) has lambda but NOT define

Discussion 12 The MCE (solutions)

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

A Brief Introduction to Scheme (II)

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

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

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

Functional Programming. Pure Functional Programming

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

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

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

CS 314 Principles of Programming Languages. Lecture 16

Documentation for LISP in BASIC

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Lexical vs. Dynamic Scope

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

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

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

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

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

SCHEME AND CALCULATOR 5b

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

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

Turtles All The Way Down

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

Sample Final Exam Questions

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

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

CSc 520 Principles of Programming Languages

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

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

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

Building a system for symbolic differentiation

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

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

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

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

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

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

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

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

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

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

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

Functional Programming. Pure Functional Languages

Scheme Quick Reference

4.2 Variations on a Scheme -- Lazy Evaluation

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

User-defined Functions. Conditional Expressions in Scheme

Functional Programming. Pure Functional Languages

Scheme Quick Reference

Lisp Basic Example Test Questions

Project 2: Scheme Interpreter

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Midterm Examination (Sample Solutions), Cmput 325

From Syntactic Sugar to the Syntactic Meth Lab:

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

An Explicit Continuation Evaluator for Scheme

April 2 to April 4, 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))

CSE341 Spring 2017, Final Examination June 8, 2017

Programming Languages

University of Massachusetts Lowell

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 314 Principles of Programming Languages

C311 Lab #3 Representation Independence: Representation Independent Interpreters

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

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

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?

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

LECTURE 16. Functional Programming

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

CS 314 Principles of Programming Languages

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Scheme: Strings Scheme: I/O

An Introduction to Scheme

Vectors in Scheme. Data Structures in Scheme

Evaluating Scheme Expressions

Functional Programming

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,

6.945 Adventures in Advanced Symbolic Programming

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

CSE341 Spring 2017, Final Examination June 8, 2017

Lecture Notes on Lisp A Brief Introduction

CSC 533: Programming Languages. Spring 2015

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

FP Foundations, Scheme

COP4020 Programming Assignment 1 - Spring 2011

Berkeley Scheme s OOP

Introduction to Scheme

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

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

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

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

Transcription:

Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the Scheme interpreter print in response to each of the following expressions? If the return value is a pair, then draw the corresponding box and pointer diagram. 1. ((caddr (1 + * 2)) 3 4) 2. (list? (cons 1 (cons 2 (cons nil nil)))) 3. (list (list (list (cons 1 nil)))) 4. (list one (cons foo (list ()))) 5. (filter number? (1 (a 2) ((b) 3) c 4 d)) 6. (cddadr (a (((b)) c d e f) g)) 7. (map (lambda (x) (cons foo x)) (1 (2) (3)))

Spring 2006 2 Lists 1. Write a procedure, (flatten ls) that takes in a deep list and flattens all the elements into a flat list. For example, (flatten (1 (2 (3 4 ((5 6) 7) 8) 9) 10)) ==> (1 2 3 4 5 6 7 8 9 10) 2. Write a predicate procedure deep-car? that takes a symbol and a deep-list (possibly including sublists to any depth) as its arguments. It should return true if and only if the symbol is the car of the list or of some list thats an element, or an element of an element, etc. > (deep - car? \ a \ (a b c)) #t > (deep - car? \ a \ (b (c a) a d)) #f > (deep - car? \ a \ ((x y) (z (a b) c) d)) #t Fill in the blanks to complete the definition. ( define (deep - car? symbol lst ) (if ( pair? lst ) (or (eq? symbol ) ( helper symbol )) ) ( define ( helper symbol lsts ) ( cond (( null? lsts ) ) (( deep - car? symbol ( car lsts )) ) ( else )))

Spring 2006 3 Trees 1. Write a procedure, (tree-member? x tree) that takes in an element and a general tree, and returns #t if x is part of tree, and #f otherwise. 2. Write double-datum?, which takes a tree as its argument, and returns true if there exists a node anywhere in the tree that has the same datum as one of its children (immediate children), otherwise false.

Spring 2006 4 Scheme-1 Here is code for scheme-1 ( define (eval -1 exp ) ( cond (( constant? exp ) exp ) (( symbol? exp ) ( eval exp )) (( quote - exp? exp ) ( cadr exp )) ((if - exp? exp ) (if (eval -1 ( cadr exp )) (eval -1 ( caddr exp )) (eval -1 ( cadddr exp )))) (( lambda - exp? exp ) exp ) (( pair? exp ) ( apply -1 (eval -1 ( car exp )) ( map eval -1 ( cdr exp )))) ( else ( error " bad expr : " exp )))) ( define ( apply -1 proc args ) ( cond (( procedure? proc ) ( apply proc args )) (( lambda - exp? proc ) ( eval -1 ( substitute ( caddr proc ) ; the body ( cadr proc ) ; the formal parameters args ; the actual arguments ()))) ; bound - vars ( else ( error " bad proc : " proc )))) 1. Suppose we moved the moved the cond clause beginning with pair? (the one that handles function calls) to the beginning of the cond, making it the first clause: ( define (eval -1 exp ) ( cond (( pair? exp )...) (( constant? exp )...) (( symbol? exp )...)... ( else ( error...)))) With the above changes, show what Scheme-1 would print given the following inputs. If the result is an error, just write error. Scheme-1: 3 Scheme-1: + Scheme-1: (if #t 1 2) Scheme-1: ((lambda (x) (* x x)) 3) Scheme-1: (+ 3 4) Scheme-1: (lambda (x) (* x x))

Spring 2006 5 2. Suppose we changed it so that we no longer called substitute inside of apply-1: ( define ( apply -1 proc args ) ( cond (( procedure? proc ) ( apply proc args )) (( lambda - exp? proc ) ( eval -1 ( caddr proc ))) ; evaluate the body without using substitute ( else ( error " bad proc : " proc )))) With the above changes, show what Scheme-1 would print given the following inputs. If the result is an error, just write error. Scheme-1: ((lambda (x) (* x x)) 3) Scheme-1: ((lambda (x) (+ 2 3)) 2) Scheme-1: (+ 3 4) Scheme-1: (lambda (x) (* x x)) Scheme-1: (lambda (x) (+ 3 4)) Scheme-1: ((lambda (cons) (cons 1 2)) +)

Spring 2006 6 Data Directed Programming and Message Passing 1. Instead of attaching one tag to an ob ject, suppose we wanted to attach multiple tags. After all, a tomato is not only a food, its also a projectile! So well rename attach-tag to attach-tags (taking a list of tags as its first argument), and rename type-tag to type-tags. Here is the original version of operate: ( define ( operate op obj ) ( let (( proc ( get (type - tag obj ) op ))) (if proc ( proc ( contents obj )) ( error " No such operator for this type ")))) Rewrite operate so that it looks for each of an objects type tags in the table, using the first one for which a procedure is found for this operator. 2. Below are definitions of m-car and m-cdr (the m here stands for message-passing). (define (m-car pair) (pair car)) (define (m-cdr pair) (pair cdr)) (a) Write a definition of m-cons that behaves just like regular cons when used in conjunction with m-car and m-cdr. (b) If you type list1 at the STk prompt, what would it print out? (define list1 (m-cons 1 (m-cons 2 (m-cons 3 ())))) (c) We would like to be able to convert a message-passing list into an ordinary Scheme list. Write the function m-list-to-regular-list that takes a message-passing list (hmm, I havent yet defined what a message-passing list is; how should it be defined?) and returns a regular list of the same elements.