CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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

CSE413 Midterm. Question Max Points Total 100

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

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

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

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

April 2 to April 4, 2018

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.001 recitation 3/21/07

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

Introduction to Scheme

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

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

Fall 2017 December 4, 2017

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

Fall 2017 December 4, Scheme. Instructions

Functional Programming. Pure Functional Languages

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

Structure and Interpretation of Computer Programs

Organization of Programming Languages CS3200/5200N. Lecture 11

YOUR NAME PLEASE: *** SOLUTIONS ***

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

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

Comp 311: Sample Midterm Examination

University of Massachusetts Lowell

CS 415 Midterm Exam Spring 2002

Functional Programming. Pure Functional Languages

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

CS 314 Principles of Programming Languages. Lecture 16

Problem Set 4: Streams and Lazy Evaluation

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

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

CSCC24 Functional Programming Scheme Part 2

Box-and-arrow Diagrams

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Lambda Calculus see notes on Lambda Calculus

CS 275 Name Final Exam Solutions December 16, 2016

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

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 as implemented by Racket

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

Functional Programming. Big Picture. Design of Programming Languages

FP Foundations, Scheme

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

An Explicit-Continuation Metacircular Evaluator

Lisp Basic Example Test Questions

Documentation for LISP in BASIC

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

Recursion & Iteration

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

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

Administrivia. Simple data types

Warm-up and Memoization

Functional Programming - 2. Higher Order Functions

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Class 6: Efficiency in Scheme

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

MIDTERM EXAMINATION - CS130 - Spring 2005

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

Sample Final Exam Questions

CS61A Midterm 2 Review (v1.1)

CSC 533: Programming Languages. Spring 2015

Functional Programming. Pure Functional Programming

Structure and Interpretation of Computer Programs

Programming Languages

6.001: Structure and Interpretation of Computer Programs

An Introduction to Scheme

SCHEME AND CALCULATOR 5b

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

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

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Introduction to Functional Programming

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

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

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

Project 2: Scheme Interpreter

UMBC CMSC 331 Final Exam

(scheme-1) has lambda but NOT define

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

Repetition Through Recursion

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Structure and Interpretation of Computer Programs

Functional programming with Common Lisp

LECTURE 16. Functional Programming

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

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

CS 314 Principles of Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSC324 Functional Programming Efficiency Issues, Parameter Lists

CS 314 Principles of Programming Languages

Notes on Higher Order Programming in Scheme. by Alexander Stepanov


Discussion 4. Data Abstraction and Sequences

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

Sequentialising a concurrent program using continuation-passing style

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

Structure and Interpretation of Computer Programs

Introduction. chapter Functions

Transcription:

COMPUTER SCIENCE 320 CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination FRIDAY, MARCH 3, 2006 Problem 1. [25 pts.] A special form is an expression that is not evaluated according to the usual rules in Scheme. Syntactic sugar is an expression that is not part of the language, but is transformed before evaluation into something that is a legal expression. For each of the following, state whether it is a special form, or syntatic sugar, or neither. If it is a special form, state why it must be a special form. If it is sytatic sugar, state what it is transformed to before evaluation. (define foo 17) Special Form, because foo is not evaluated. (car (1 b 3)) Neither. (lambda (x) (+ 3 x)) Special Form, because (x) is not evaluated. (let ((y 10)) (* 5 y)) Syntactic Sugar. Becomes: ((lambda (y) (* 5 y)) 10) (if (= a b) (/ 1 0) (+ 1 0)) Special Form, because (/ 1 0) is not evaluated (in this case). (letrec ((a 1) (b 2)) (* a b)) Special Form, because the definitions of a and b are in the enclosing environment. (mapcar car ((1 3) (4 5))) Neither. (define (sum a b) (+ a b)) Syntactic Sugar. Becomes: (define sum (lambda (a b) (+ a b))) (+ a 12) Neither (cond ((= a 1) (foo)) ((= a 0) 20) (bar)) Special Form, because some expressions may not be evaluated. 1

Problem 2. (a) [11 points.] An accumulator is a procedure that is called repeatedly with a single numeric argument and accumulates its arguments into a sum. Each time it is called, it returns the currently accumulated sum. Write a procedure make-accumulator that generates accumulators, each maintaining an independent sum. The input to make-accumulator should specify the initial value of the sum. For example: (define A (make-accumulator 5)) 15 25 (define (make-accumulator initial-value) (let ((sum initial-value)) (set! sum (+ sum addend)) sum)) or equivalent de-sugared expressions. (b) [11 points.] Now modify make-accumulator so that if the accumulator is called with the special value reset, it resets the sum to the original value. For example: (define A (make-accumulator 5)) 15 25 (A reset) 5 (define (make-accumulator initial-value) (let ((sum initial-value) (iv initial-value)) (lambda (addend) (if (equal? addend reset) (set! sum iv) (set! sum (+ sum addend))) sum))) or equivalent de-sugared expressions. (c) [3 points] Explain why the example given above of how A works tells you that A cannot be a functional program. 2

Problem 3. A ring is a list in which the tail pointer of the last element points to the first element of the list. For example: myring a friend of (a) [4 points] Assume myring is defined to be the above data structure. What happens if you type myring to the scheme interpreter? prints out a friend of a friend of a friend of a friend of... forever (b) [7 points] Write a procedure make-ring! that takes a list and makes it a ring (without using cons or append). For example, (define myring (make-ring! (a friend of))) yields the ring above. (define (helper-fn list head) (if (= 1 (length list)) (set-cdr! list head) (helper-fn (cdr list) head))) (define (make-ring! list) (helper-fn list list) list) 3

(c) [7 points] Write a procedure that prints out each element of the ring only once (using display) in forward order. For example: (display-ring myring) a friend of (hint: eq? tests if two pointers are the same.) (define (display-ring ring) (define (helper-fn2 head ring) (begin (display (car ring)) (display " ") (if (not (eq? head (cdr ring))) (helper-fn2 head (cdr ring)) ()))) (helper-fn2 ring ring)) (d) [7 points] Write a procedure that deletes the first occurrence of an item from the ring, leaving it a ring, using list mutator functions such as set-car! and set-cdr!. You can assume that at least one instance of the item is in fact in the ring. For example, (set! myring (delete-ring! a myring)) (display-ring myring) friend of (define (delete-ring! item ring) (if (equal? item (car (cdr ring))) (begin (set-cdr! ring (cdr (cdr ring))) (cdr ring)) (delete-ring! item (cdr ring)))) 4

Problem 4. We have studied the higher order function accumulate. For example (accumulate + 0 list) returns the sum of the items in list and (accumulate append () ((1 2) (3 4) (5 6)) returns (1 2 3 4 5 6) (a) [15 points] Write accumulate as a recursive function. It doesn t matter what order you use to perform the accumulation. (define (accumulate fn initial-value inputlist) (if (null? inputlist) initial-value (fn (car inputlist) (accumulate fn initial-value (cdr inputlist))))) (b) [10 points] Write accumulate as an iterative (imperative) function. It doesn t matter what order you use to perform the accumulation. (define (accumulate fn initial-value inputlist) (let ((list inputlist) (result initial-value)) (while ((not (null? list))) (begin (set! result (fn result (car lst)) (set! list (cdr list))))) result)) 5

Extra Credit [5 points] Yesterday the ACM announced that Peter Naur is the Winner of this year s Turing Award, the Nobel Prize of Computer Science (no kidding!). What is Peter Naur most famous for? (hint: along with John Backus). Algol-60 and/or Backus-Naur Form (BNF) 6