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

Similar documents
UNIVERSITY OF TORONTO Faculty of Arts and Science

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

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

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

YOUR NAME PLEASE: *** SOLUTIONS ***

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

UNIVERSITY OF TORONTO Faculty of Arts and Science

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

CPSC 311, 2010W1 Midterm Exam #2

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Functional Programming. Pure Functional Programming

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

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

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

CSE413 Midterm. Question Max Points Total 100

CSE 341 Section Handout #6 Cheat Sheet

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

cs173: Programming Languages Midterm Exam

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

The Typed Racket Guide

CS 360 Programming Languages Interpreters

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

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

Box-and-arrow Diagrams

Working with recursion

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Functional Programming. Pure Functional Languages

Scheme Quick Reference

Functional Programming Mid-term exam Tuesday 3/10/2017

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Discussion 4. Data Abstraction and Sequences

Module 8: Local and functional abstraction

SCHEME AND CALCULATOR 5b

Principles of Programming Languages 2017W, Functional Programming

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

User-defined Functions. Conditional Expressions in Scheme

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

Scheme Quick Reference

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

Structure and Interpretation of Computer Programs

CSE 341: Programming Languages

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Questions & Answers 28 Sept.


CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CS558 Programming Languages

CSCC24 Functional Programming Scheme Part 2

CS 314 Principles of Programming Languages. Lecture 16

Typed Racket: Racket with Static Types

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Comp 311: Sample Midterm Examination

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

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

CS 320: Concepts of Programming Languages

A brief tour of history

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Operational Semantics

MIDTERM EXAMINATION - CS130 - Spring 2003

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CS558 Programming Languages

CS 415 Midterm Exam Spring 2002

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

Scheme: Strings Scheme: I/O

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Lecture 4: Higher Order Functions

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

Programming Paradigms Written Exam

Programming Paradigms Written Exam (6 CPs)

Project Compiler. CS031 TA Help Session November 28, 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

EXAMINATION INSTRUCTIONS

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

COMP520 - GoLite Type Checking Specification

Midterm Examination COMP 304B 2004: Object-oriented Design

UNIVERSITY REGULATIONS

Problem Score Max Score 1 Syntax directed translation & type

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CS61A Midterm 2 Review (v1.1)

Racket: Modules, Contracts, Languages

Consider the following EBNF definition of a small language:

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

UMBC CMSC 331 Final Exam

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

CSE341 Autumn 2017, Final Examination December 12, 2017

Transcription:

UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm Sample s CSC324H1 Duration: 50 minutes Instructor(s): David Liu. No Aids Allowed Name: Student Number: Please read the following guidelines carefully. Please print your name and student number on the front of the exam. This examination has 4 questions. There are a total of 8 pages, DOUBLE-SIDED. You may always write helper functions unless asked not to. Documentation is not required unless asked for. Answer questions clearly and completely. Provide justification unless explicitly asked not to. Take a deep breath. This is your chance to show us How much you ve learned. We WANT to give you the credit That you ve earned. A number does not define you. Question Grade Out of Q1 8 Q2 6 Q3 6 Q4 7 Total 27

1. [8 marks] Programming language concepts. (a) [2 marks] Here is a grammar describing a simple arithmetic language. <expr> represents a valid expression in the language. <expr> = <times-expr> <plus-expr> NUMBER <times-expr> = (* <plus-expr> <plus-expr> ) <plus-expr> = (+ NUMBER NUMBER) Which of the following are valid expressions in this language? Circle all that apply. No explanation necessary. (* 20 50) (+ 20 50) (* (+ 30 40) (+ 60 3)) 17 (+ 20 50), (* (+ 30 40) (+ 60 3)), and 17 are valid. (b) [2 marks] Circle all the Haskell functions below that are tail-recursive. No explanation is necessary. f x = if x == 0 then 10 f (x - 1) f 0 = 10 f 1 = 20 f x = (f (x - 1)) + (f (x - 2)) f 0 = 10 f x = if mod x 2 == 0 then 1 + (f (x - 1)) f (x - 1) f 0 = 10 f x = if mod x 2 == 0 then f (x - 2) f (x - 1) Top-left and bottom-right are tail-recursive. (c) [2 marks] Beneath each Racket code snippet, write down the value obtained when you evaluate it. If there would be an error, briefly explain the error. Page 2/8

; Snippet 1 (define x 10) (define f1 (lambda (n) (* x n))) (let* ([x 2] [n 3]) (f1 1)) 10 ; Snippet 2 (define f2 (lambda (n) (* y n))) (let* ([y 2] [n 3]) (f 1)) There s an error: y is unbound in the closure of f2. (d) [2 marks] Suppose we define the following functions in Racket and Haskell: ; Racket version (define (apply-if f condition x) (if condition (f x) (f 0))) -- Haskell version applyif f condition x = if condition then f x f 0 Explain why these functions are not equivalent in Racket and Haskell. Include a concrete code example in your explanation. Racket evaluates function call arguments eagerly (before calling the function), while Haskell doesn t. If condition is false, x doesn t need to be evaluated in Haskell, but it still does in Racket. So (apply-if identity #f (error "e")) would raise an error in Racket, but applyif id False (error "e") would evaluate to 0 in Haskell. Page 3/8

2. [6 marks] Functional programming. Consider the Racket function indexes-where, which takes exactly two arguments: A unary predicate (returns a boolean) pred. A list lst, where every element is a valid input to pred. indexes-where returns a list containing the indexes of the elements of lst that satisfy pred. > (indexes-where (lambda (s) (> (string-length s) 3)) (list "hi" "goodbye" "" "a" "hahaha")) '(1 4) Implement this function in Racket. The only list functions you may use are the ones found on the aid sheet. You may use any combination of pattern-matching, explicit recursion, and higher-order list functions. Hint: define a helper function with an extra parameter that keeps track of the current index. ; Version 1, using explicit recursion (define (indexes-where pred lst) (indexes-where-helper pred lst 0)) (define (indexes-where-helper pred lst i) (if (null? lst) null (let* ([x (first lst)] [rest-indexes (indexes-where-helper pred (rest lst) (+ i 1))]) (if (pred x) (cons i rest-indexes) rest-indexes)))) ; Version 2, using foldl ; It's a bit harder to use foldl, because we want to accumulate both ; the list of indexes *and* the current index, *and* we want ; the "update" function to have pred in scope. (define (indexes-where1 pred lst) (first (foldl (indexes-where-helper1 pred) (list null 0) lst))) ; A higher-order function that takes a predicate and returns the ; actual update function. (define (indexes-where-helper1 pred) ; The accumulator is a list of two values: a list of indexes ; and the current index. (lambda (x acc) (let* ([indexes (first acc)] [i (second acc)]) (if (pred x) ; Note that we need to add i to the *back* of the accumulated list. (list (append indexes (list i)) (+ i 1)) (list indexes (+ i 1)))))) Page 4/8

3. [6 marks] Macros. (a) [3 marks] Consider the following macro: (define-syntax my-macro (syntax-rules () [(my-macro <a>... <b>) (list (<a> <b>)...)])) Write an expression using this macro that produces the list (10 20) when evaluated. (my-macro (lambda (x) (+ 10 x)) (lambda (x) (+ 20 x)) 0) Write an expression using this macro that would result in a runtime error when evaluated. Briefly explain what error would occur. (my-macro first 1) This expands into (list (first 1)), which raises a runtime error since first expects a list, not a number. (b) [2 marks] Consider the my-class macro on the aid sheet, and the sample class Point defined below it. Suppose we evaluate the definition (define p (Point 3 4)). (i) In Racket, p is technically a function. How many arguments does p take? 1 (the message being sent, i.e., the attribute to lookup) (ii) Write an expression to access the y attribute of p. (p y) (iii) What is the difference between the expressions (p size) and ((p size))? (p size) refers to a function (the size method ), while ((p size)) calls the function, and returns the size of the point. (c) [1 mark] Suppose we switch the order of attribute lookup in the my-class macro, so that class dict is searched before self dict. Explain how this would change the behaviour of the my-class macro. This means that when we define a class that has an attribute and method with the same name, the method is the one returned when we send a message with that name to instances of the class. We could say that the method name shadows the attribute name. Page 5/8

4. [7 marks] Abstract Syntax Trees. Here is a Haskell definition of small arithmetic expression language. data Expr = IntLiteral Int -- An integer literal Identifier String -- An identifier Plus [Expr] -- The sum of an arbitrary number of expressions -- Example expressions Plus [IntLiteral 3, IntLiteral 5] -- (+ 3 5) Plus [IntLiteral 10, Identifier "x", Identifier "y"] -- (+ 10 x y) Plus [Plus [IntLiteral 4], Plus [IntLiteral 10, Identifier "a"]] -- (+ (+ 4) (+ 10 a)) For all parts of this question, the only list functions you may use are the ones found on the aid sheet, and concat, which takes a list of lists and flattens them into a single list. (a) [3 marks] Implement the following Haskell function. We have started the pattern-matching for you. extractprimitives :: Expr -> [Expr] extractprimitives (IntLiteral n) = [IntLiteral n] extractprimitives (Identifier s) = [Identifier s] extractprimitives (Plus args) = concat (map extractprimitives args) (b) [2 marks] Because of the properties of addition, it is possible to (statically) flatten nested Plus expressions without changing their value. For example, Plus [Plus [IntLiteral 4], Plus [IntLiteral 10, Identifier "a"]] can be simplified to Plus [IntLiteral 4, IntLiteral 10, Identifier "a"] Using extractprimitives or otherwise, implement the function on the next page, which performs this simplification. flattenplus :: Expr -> Expr flattenplus (Plus args) = Plus (extractprimitives (Plus args)) -- Or, using concat again flattenplus (Plus args) = Plus (concat (map extractprimitives args)) (c) [2 marks] Warning: this is a more challenging part, and not worth much. Please only attempt this after completing the rest of this test. A further simplification we can make to a Plus expression is to compute the sum of all IntLiterals in the expression, so that no more than one IntLiteral appears in a single Plus expression. Implement the following function, which performs this simplification. There are a few different possible ways of doing this. The implementation below basically divides args into the IntLiteral and Identifier parts, computes on each part separately, and then puts them back together into a new Plus expression. combineliterals :: Expr -> Expr combineliterals (Plus args) = Page 6/8

let ints = extractints args ids = filter isidentifier args in if ints == [] then Plus ids Plus (IntLiteral (foldl (+) 0 ints) : ids) extractints :: [Expr] -> [Int] extractints [] = [] extractints (IntLiteral n: exprs) = n : extractints exprs extractints (_ : exprs) = extractints exprs isidentifier :: Expr -> Bool isidentifier (Identifier _) = True isidentifier _ = False Page 7/8

Use this page for rough work. If you want work on this page to be marked, please indicate this clearly at the location of the original question. Total Pages = 8. Total Marks = 27. Page 8/8