CS 275 Name Final Exam Solutions December 16, 2016

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

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

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

Functional Programming. Pure Functional Programming

Project 2: Scheme Interpreter

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

Lexical vs. Dynamic Scope

Functional Programming - 2. Higher Order Functions

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

Discussion 12 The MCE (solutions)

Functional Programming. Pure Functional Languages

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

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

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

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

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

Functional Programming. Pure Functional Languages

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

CSE341 Spring 2017, Final Examination June 8, 2017

An Explicit-Continuation Metacircular Evaluator

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?


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

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

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

CS 314 Principles of Programming Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

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

CSE341 Spring 2017, Final Examination June 8, 2017

Building a system for symbolic differentiation

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSc 520 Principles of Programming Languages

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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?

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

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

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

CS 360 Programming Languages Interpreters

CSE341 Autumn 2017, Final Examination December 12, 2017

Comp 311: Sample Midterm Examination

A Brief Introduction to Scheme (II)

CS61A Midterm 2 Review (v1.1)

SCHEME AND CALCULATOR 5b

(scheme-1) has lambda but NOT define

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

CS 314 Principles of Programming Languages. Lecture 16

Introduction to Scheme

From Syntactic Sugar to the Syntactic Meth Lab:

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

CS 415 Midterm Exam Fall 2003

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

CS 415 Midterm Exam Spring SOLUTION

User-defined Functions. Conditional Expressions in Scheme

CS 314 Principles of Programming Languages

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

Structure and Interpretation of Computer Programs

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

CSE341, Spring 2013, Final Examination June 13, 2013

CS 314 Principles of Programming Languages

Scheme Quick Reference

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

LECTURE 16. Functional Programming

Documentation for LISP in BASIC

Module 10: General trees

Scheme Quick Reference

Lisp. Versions of LISP

Turtles All The Way Down

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

Building a system for symbolic differentiation

Programming Languages

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Structure and Interpretation of Computer Programs

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

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

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

Data abstraction, revisited

Warm-up and Memoization

CS 61A Midterm #2 - October 5, 1998

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CS 415 Midterm Exam Spring 2002

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

Lecture 3: Expressions

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

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

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

Evaluating Scheme Expressions

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

CSCC24 Functional Programming Scheme Part 2

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

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

Lisp Basic Example Test Questions

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

The Typed Racket Guide

Transcription:

CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need to write. Each numbered question is worth 10 points. Please write and sign the Honor Pledge at the end of your solutions. 1. Write (index x lat) which returns the 0-based index of item x in the flat list lat. If x is not in lat this should return -1. (define inc (lambda (t) (if (= -1 t) t (+ 1 t)))) (define index (lambda (x lat) [(null? lat) -1] [(= x (car lat)) 0] [else (inc (index x (cdr lat)))])))

2. Write procedure (SameElts? lat1 lat2) that returns #t if lat1 and lat2 have the same elements (including multiplicities) though not necessarily in the same order. Naturally, SameElts? should return #f if it doesn t return #t. For example, (SameElts? (1 2 3) (3 2 1)) returns #t while (SameElts? (1 2 3) (1 2 1 2 3)) returns #f. (define SameElts (lambda (lat1 lat2) [(null? lat1) (if (null? lat2) #t #f)] [(= (count (car lat1) lat1) (count (car lat1) lat2)) (SameElts (rember-all (car lat1) lat1) (rember-all (car lat1) lat2))] [else #f]))) (define count (lambda (x lat) [(null? lat) 0] [(eq? x (car lat)) (+ 1 (count x (cdr lat)))] [else (count x (cdr lat))]))) (define rember-all (lambda (x lat) [(null? lat) null] [(eq? x (car lat)) (rember-all x (cdr lat))] [else (cons (car lat) (rember-all x (cdr lat)))]))) 2

3. Write (equals? L1 L2) which determines if the general lists L1 and L2 are structurally identical: they have the same elements in the same structures. For example (equals? (1 (2 3 (4))) (1 (2 3) (4)))) returns #t while (equals? (1 (2 3 (4))) (1 (2 3) 4)) returns #f. (define Equals (lambda (L1 L2) [(null? L1) (if (null? L2) #t #f)] [(and (pair? L1) (pair? L2)) (and (Equals (car L1) (car L2)) (Equals (cdr L1) (cdr L2)))] [(or (pair? L1) (pair? L2)) #f] [else (eq? L1 L2)]))) 3

4. Remember the fold function: (define fold (lambda (recur base lat) (letrec ([h (lambda (s) [(null? s) base] [else (recur (car s) (h (cdr s)))]))]) (h lat)))) Use fold to write (count a lat), which returns the number of instances of atom a in lat. Note for 2018: do this with foldl or foldr. (define Count (lambda (a lat) (foldrr (lambda (x y) (if (eq? x a) (+ 1 y) y)) 0 lat))) 4

5. Here is a tree constructor: (define new-tree (lambda (v list-of-children) (list tree v list-of-children))) We ll use the null list to represent an empty tree. Define a Scheme procedure (height t) that produces the height of tree t which is built with this constructor. Remember that the height of a tree is the number of edges on the longest path from the root to a leaf. (define height (lambda (t) [(leaf? t) 0] [else (+ 1 (apply max (map height (Kids t))))]))) Here are the helper functions: (define Kids (lambda (t) (caddr t))) (define leaf? (lambda (t) (null? (Kids t)))) 5

6. We spent a lot of time talking about scoping rules. Scheme uses static (or lexical) scoping; early LISP used dynamic scoping. Explain in one or two sentences the difference between static and dynamic scoping, then give a Scheme expression that evaluates differently under the two scoping mechanisms. Say what your expression evaluates to under each mechanism. Static scoping looks for free variables in the environment in which the function is created. Dynamic scoping looks for free variables in the environment in which the function is called. (let ([A 1]) (let ([f (lambda (x) (+ x A))]) (let ([A 10]) (f 3)))) With static scoping this evaluates to 4, with dynamic scoping to 13. 6

7. You remember let*, which evaluates its bindings sequentially: (let* ([x 5] [y x]) y ) is a valid expression that evaluates to 5. What code would you use for (parse exp) and for (eval-exp tree env) to add let* to your minischeme interpreter? You can use (without redefining) any datatypes you used in your interpreter but give some kind of explanation of what you are using so I can read your code. My approach to this is to parse a let* into a nested sequence of lets, which means we have to modify the parser but not the interpreter. The parser should use the condition: [(eq? (car exp) let*) (makelet* (map car (cadr exp)) (map parse (map cadr (cadr exp)) (parse (caddr exp)))] Procedures makelet* converts the let* expression into a sequence of nested lets: (define makelet* (lambda (syms vals body) [(null? syms) body] [else (newletexp (list (car syms)) (list (car vals)) (makelet* (cdr syms) (cdr vals) body))]))) Here newletexp is the constructor for the tree data type that represents a standard let expression. It takes 3 arguments: a list of binding symbols, a list of parsed binding values, and a parsed body. 7

8. Here is a stream: (0 1 1 2 2 4 3 8 4 16 5 32 6 64...) As you can see, its values alternate between n and 2 n. Give a Scheme expression that produces this stream. (define S (lambda (n p) (cons$ n (cons$ p (S (+ n 1) (* 2 p)))))) 8

9. Write a Continuation Passing Style function called RemberBob that takes a general list argument and removes all of the instance of the atom bob. If the list contains the atom PANIC the function returns only PANIC, regardless of how deeply it is buried in the recursion when it finds this atom. Remember that CPS procedures need to be tailrecursive. For example (RemberBob (john (paul bob) ((george) ringo) bob) (lambda (y) y)) returns (john (paul) ((george) ringo)), while (RemberBob (pete (roger bob PANIC) ((keith john))) (lambda (y) y)) returns PANIC. (define RemberBob (lambda (L k) [(null? L) (k null)] [(pair? (car L)) (RemberBob (car L) (lambda (x) (RemberBob (cdr L) (lambda (y) (k (cons x y))))))] [(eq? (car L) 'bob) (RemberBob (cdr L) (lambda (x) (k x)))] [(eq? (car L) 'PANIC) 'PANIC] [else (RemberBob (cdr L) (lambda (x) (k (cons (car L) x))))]))) 9

10. What will the following procedure return if I call it with (f (3 2 1 0 1 2 3))? If your answer is correct it will get full credit, but in case your answer isn t correct it would help if you gave some explanation. (define f (lambda (lat) (call/cc (lambda (k) (let ([g (lambda (y) (call/cc (lambda (c) (if (= 1 y) (c 5) (+ y 2)))))]) [(null? lat) 0] [(= 0 (car lat)) (k 0)] [else (+ (g (car lat)) (f (cdr lat)))])))))) This evaluates to 14. (g 3) is 5, (g 2) is 4, (g 1) is 5. Procedure f sums the value of g applied to each of the elements of the lat prior to the first 0. 5+4+5 is 14 10

[This page is for scratch work.] 11

Please write and sign the Honor Pledge at the end of your solutions. 12