Sample Final Exam Questions

Similar documents
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.

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

CS61A Midterm 2 Review (v1.1)

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

April 2 to April 4, 2018

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

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

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

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

CS 314 Principles of Programming Languages. Lecture 16

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

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

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

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,

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

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

Functional Programming. Pure Functional Programming

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

Documentation for LISP in BASIC

Project 2: Scheme Interpreter

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

An Explicit-Continuation Metacircular Evaluator

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

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

Discussion 12 The MCE (solutions)

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

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

Building a system for symbolic differentiation

A Brief Introduction to Scheme (II)

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

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

Functional Programming. Pure Functional Languages


Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

CSE413 Midterm. Question Max Points Total 100

CS450 - Structure of Higher Level Languages

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

CSE341 Spring 2016, Final Examination June 6, 2016

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

Building a system for symbolic differentiation

CS 314 Principles of Programming Languages

Structure and Interpretation of Computer Programs

Lexical vs. Dynamic Scope

Structure and Interpretation of Computer Programs

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

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

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

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

CSE341 Autumn 2017, Final Examination December 12, 2017

Functional Programming. Pure Functional Languages

CSE341 Spring 2017, Final Examination June 8, 2017

CSc 520 Principles of Programming Languages

Turtles All The Way Down

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

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

Introduction to Scheme

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

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

Project 5 - The Meta-Circular Evaluator

CSE 341 Section Handout #6 Cheat Sheet

YOUR NAME PLEASE: *** SOLUTIONS ***

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

Structure and Interpretation of Computer Programs

Scheme: Strings Scheme: I/O

CS 314 Principles of Programming Languages

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

Lisp Basic Example Test Questions

CS61A Notes Disc 11: Streams Streaming Along

by the evening of Tuesday, Feb 6

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

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

Structure and Interpretation of Computer Programs

University of Massachusetts Lowell

SCHEME AND CALCULATOR 5b

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

Comp 311: Sample Midterm Examination

Lisp. Versions of LISP

Midterm Examination (Sample Solutions), Cmput 325

Project 5 - The Meta-Circular Evaluator

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Structure and Interpretation of Computer Programs

Review of Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

INTERPRETERS AND TAIL CALLS 9

Imperative languages

4.2 Variations on a Scheme -- Lazy Evaluation

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

FP Foundations, Scheme

Streams and Evalutation Strategies

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

Discussion 11. Streams

Transcription:

91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will be comprehensive, covering all topics from the course (homework, lectures and readings), but there will be a heavier emphasis on topics that were not on the first or second exam. You can bring up to 3 sheets of handwritten notes to use during the final. You don t need to copy the metacircular evaluator code; a clean copy of the mc-eval-with-let file will be handed out with the exam. Problem 1: List Mutation Suppose a Scheme interpreter evaluates the following expressions: (define (append lst1 lst2) (if (null? lst1) lst2 (cons (car lst1) (append (cdr lst1) lst2)))) (define a '(a b c)) (define b (cons a a)) (define c (append a a)) Draw the box and pointer representation of the data structures a, b, and c. What would the Scheme interpreter print as the value of b? The Scheme interpreter now evaluates the following expressions: (set-cdr! (cdr b) nil) (set-cdr! (cdr c) a) Draw the box and pointer representation of the data structures a, b, and c after these expressions are interpreted. What would the Scheme interpreter now print as the value of b?

2 Problem 2: Streams Consider the following definitions for add-streams and mul-streams. (define (add-streams s1 s2) (cons-stream (+ (stream-car s1) (stream-car s2)) (add-streams (stream-cdr s1) (stream-cdr s2)))) (define (mul-streams s1 s2) (cons-stream (* (stream-car s1) (stream-car s2)) (mul-streams (stream-cdr s1) (stream-cdr s2)))) Capture the pattern in the definitions above by writing map-two-streams, which takes a function and two streams as parameters. Use map-two-streams to define min-two-streams, which takes two streams and returns a stream containing the minimum values of the corresponding elements of the stream. For example, (min-two-streams integers twos) would return 1 2 2 2 2 2 2 2...

3 Problem 2 continued: The following stream definitions are used to define the stream of factorials. (define ones (cons-stream 1 ones)) (define integers (cons-stream 1 (add-streams ones integers))) (define fact (cons-stream 1 (mul-streams fact integers))) How many multiplications are performed when computing the n th factorial value in the fact stream? Explain your answer. If there were no memoization in Scheme, how many multiplications would be performed when computing the n th factorial value in the fact stream? Explain your answer.

4 Problem 3: Object Oriented Scheme Refer to the game.scm handout for this problem. Let s create a new type of person in our system, called an opl-lecturer. The opllecturer should be able to bring-cookies-to-exam. Additionally, the opl-lecturer should say Scheme is fun after anything that she is asked to say. Fill in the blanks in the following procedure. Assume that cookie-store is a magical place that will continually manufacture cookies. (define (make-opl-lecturer name birthplace threshold) (let (<exp1>) (lambda (message) (cond (<exp2> (lambda (self) (let ((location (ask self 'place))) (ask self 'move-to cookie-store) (ask self 'take cookies) (ask self 'move-to location)))) ((eq? message 'say) <exp3>) (else <exp4>))))) Write your answers here. <exp1> <exp2> <exp3> <exp4>

5 Problem 4: Metacircular Evaluator Changing the syntax of our language After spending a semester writing prefix expressions, Louis Reasoner decides that he would like to change the language of our metacircular interpreter to accept infix expressions for uml:+, uml:-, uml:*, and uml:/, where there are only two arguments. For example, instead of writing (uml:+ x 2), we would now write (x uml:+ 2). There will be no change to how we call other procedures. Refer to the mc-eval-with-let file for writing your solution. If you are adding code to the system, please write it below. If it is a line of code to be inserted into an existing procedure, be specific and clear about where it should be inserted. If you are modifying existing procedures, it would probably be clearer if you were to rewrite the procedure here.

6 Problem 5: Metacircular Evaluator Changing the way we store variables and values in an environment frame In the metacircular evaluator, variables and values are simply consed together in an environment frame, using make-frame: (define (make-frame variables values) (cons variables values)) Rewrite make-frame so that the variables and values are paired in cons cells, with each cons cell the element of a list. Recall that extend-environment checks to see if there are the same number of variables and values, so you do not need to do this in make-frame. For example, evaluating (make-frame (a b c) (1 2 3)) would return the list ((a. 1) (b. 2) (c. 3)) How would you need to change lookup-variable-value to work with our new method of storing variables and values? Write the code here. Is there anything else in the metacircular interpreter that would need to be changed for our new method for storing variables and values in frames? You do not need to write the code if any changes are necessary. Just state what needs to be done in existing procedure(s), if applicable.