Special Directions for this Test

Similar documents
Test on Declarative Concurrency, the Message Passing Model, and Programming Models vs. Problems

Special Directions for this Test

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

An Oz Subset. 1 Microsyntax for An Oz Subset. Notational Conventions. COP 4020 Programming Languages 1 January 17, 2012

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

Programming Language Concepts, cs2104 Lecture 04 ( )

Lecture 21: Relational Programming II. November 15th, 2011

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

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

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

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Special Directions for this Test

Mini-ML. CS 502 Lecture 2 8/28/08

CSE P 501 Exam 8/5/04

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

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

University of Massachusetts Lowell

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Exam for 2G1512 DatalogiII, Apr 12th

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

CMSC 330: Organization of Programming Languages. Operational Semantics

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

G Programming Languages - Fall 2012

Hofl, a Higher-order Functional Language. 1 An Overview of Hofl. 2 Abstractions and Function Applications

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

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

Sample Final Exam Questions

CSE-321 Programming Languages 2012 Midterm

CSCC24 Functional Programming Scheme Part 2

Begin at the beginning

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

Homework 3 COSE212, Fall 2018

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

Repetition Structures

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Chapter 1. Fundamentals of Higher Order Programming

CSE P 501 Exam 12/1/11

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CS 3360 Design and Implementation of Programming Languages. Exam 1

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Lists. Michael P. Fourman. February 2, 2010

Declarative concurrency. March 3, 2014

Recap: Functions as first-class values

INF121: Functional Algorithmic and Programming

Functional Programming. Pure Functional Languages

Haskell Overview II (2A) Young Won Lim 8/9/16

CS 340 Spring 2019 Midterm Exam

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

DO NOT OPEN UNTIL INSTRUCTED

Working with recursion

CS 440: Programming Languages and Translators, Spring 2019 Mon

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

Datatype declarations

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CSCE 314 Programming Languages

CSCI-GA Scripting Languages

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

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

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Shell CSCE 314 TAMU. Higher Order Functions

Types and Programming Languages. Lecture 5. Extensions of simple types

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

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

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Functional Programming

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

Functional Programming and Haskell

Functional Programming. Pure Functional Languages

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Organization of Programming Languages CS3200/5200N. Lecture 11

Local defini1ons. Func1on mul1ples- of

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

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

Functional Programming Languages (FPL)

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

COS 126 General Computer Science Spring Written Exam 1

CSE 130 [Winter 2014] Programming Languages

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

by the evening of Tuesday, Feb 6

Chapter 9 Introduction to Arrays. Fundamentals of Java

CS205: Scalable Software Systems

CMSC 330: Organization of Programming Languages. Lambda Calculus

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Lambda Calculus. Gunnar Gotshalks LC-1

FOFL and FOBS: First-Order Functions

Shell CSCE 314 TAMU. Haskell Functions

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Documentation for LISP in BASIC

Lazy Functional Programming in Haskell

Transcription:

1 Fall, 2007 Name: COP 4020 Programming Languages 1 Makeup Test on Declarative Programming Techniques Special Directions for this Test This test has 8 questions and pages numbered 1 through 9. This test is open book and notes. If you need more space, use the back of a page. Note when you do that on the front. Before you begin, please take a moment to look over the entire test so that you can budget your time. Clarity is important; if your programs are sloppy and hard to read, you may lose some points. Correct syntax also makes a difference for programming questions. When you write Oz code on this test, you may use anything in the declarative model (as in chapters 2 3 of our textbook). So you must not use imperative features (such as cells and assignment). You are encouraged to define functions or procedures not specifically asked for if they are useful to your programming; however, if they are not in the Oz base environment, then you must write them into your test. (This means you can use things in the Oz base environment such as Map, FoldR, Filter, App, etc.) For Grading Problem Points Score 1 5 2 10 3 5 4 10 5 10 6 10 7 25 8 25

2 1. (5 points) In the following statement, written in the kernel language of the declarative model, circle each free variable identifier occurrence (and only the free variable identifier occurrences). ProdFuns = proc {$ Ls Acc Return} case Ls of Hd Tail then local Temp in Temp = proc {$ X R} {Prod Hd X R} local Temp2 in Temp2 = Temp Acc {ProdFuns Tail Temp2 Return} else {Reverse Acc Return} 2. (10 points) The following is another copy of the same kernel language code as above. On the following copy, circle each bound variable identifier occurrence (and only the bound variable identifier occurrences). ProdFuns = proc {$ Ls Acc Return} case Ls of Hd Tail then local Temp in Temp = proc {$ X R} {Prod Hd X R} local Temp2 in Temp2 = Temp Acc {ProdFuns Tail Temp2 Return} else {Reverse Acc Return}

3 3. (5 points) Desugar the following Oz code into kernel syntax by expanding all syntactic sugars. (Assume that Times is a function that is declared elsewhere.) fun {By2 I} {Times I 2}

4 4. (10 points) Write a function EvenNumbers: <fun {$ <List Integer>}: <List Integer> that takes a list of integers LoN and returns a list of the even integers in LoN, in their original order. The following are examples, that use the Test procedure from the homework. {Test {EvenNumbers nil} == nil} {Test {EvenNumbers [2]} == [2]} {Test {EvenNumbers [3]} == nil} {Test {EvenNumbers [1 2 3 4 5 6 7 6 2 3 1]} == [2 4 6 6 2]} {Test {EvenNumbers [2 3 4 5 6 7 6 2 3 1]} == [2 4 6 6 2]} {Test {EvenNumbers [3 4 5 6 7 6 2 3 1]} == [4 6 6 2]}

5 5. (10 points) Write a function SumSquares: <fun {$ <List Number>}: <Number> that takes a list of numbers, LoN, and returns the sum of their squares. Your solution must have iterative behavior, and must be written using tail recursion. Don t use any higher-order functions in your solution. (You are supposed to know what these directions mean.) The following are examples, that use the Test procedure from the homework. {Test {SumSquares nil} == 0} {Test {SumSquares [5]} == 25} {Test {SumSquares [5 10]} == 125} {Test {SumSquares [1 2 3 4]} == 30} {Test {SumSquares [7 5 10 12 ~7 ~5 ~10 ~12]} == 636} {Test {SumSquares [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]} == 0} 6. (10 points) Write a definition of SumSquares that solves the problem given above, but using FoldL. Do this by filling in the blanks in the following code outline. (Note that these blanks may be larger in size than you need.) fun {SumSquares LoN} {FoldL }

6 7. (25 points) This problem is about sales data records. Sales data records are defined by the following grammar. SalesData ::= store(address: String amounts: List Int ) group(name: String members: List SalesData ) Write a function NewAddress: <fun {$ <SalesData> <String> <String>}: <SalesData> that takes a sales data record SD, two strings New and Old, and returns a sales data record that is just like SD except that all store records in SD whose address field s value is (== to) Old in SD are changed to New in the result. The following are examples using the Test function from the homework. {Test {NewAddress group(name: "StartUP!" members: nil) "Downtown" "50 Washington Ave."} == group(name: "StartUP!" members: nil)} {Test {NewAddress store(address: "The Mall" amounts: [10 32 55]) "110 Main St." "The Mall"} == store(address: "110 Main St." amounts: [10 32 55])} {Test {NewAddress group(name: "Target" members: [store(address: "The Mall" amounts: [10 32 55])]) "NewAddress" "OldAddress"} == group(name: "Target" members: [store(address: "The Mall" amounts: [10 32 55])])} {Test {NewAddress group(name: "Target" members: [store(address: "The Mall" amounts: [10 32 55]) store(address: "Downtown" amounts: [4 0 2 0])]) "253 Sears Tower" "The Mall"} == group(name: "Target" members: [store(address: "253 Sears Tower" amounts: [10 32 55]) store(address: "Downtown" amounts: [4 0 2 0])])} {Test {NewAddress group(name: "ACME" members: [group(name: "Robucks" members: [store(address: "The Mall" amounts: [99]) store(address: "Maple St." amounts: [32])]) group(name: "Target" members: [store(address: "The Mall" amounts: [10 55]) store(address: "Downtown" amounts: [4])])]) "High St." "The Mall"} == group(name: "ACME" members: [group(name: "Robucks" members: [store(address: "High St." amounts: [99]) store(address: "Maple St." amounts: [32])]) group(name: "Target" members: [store(address: "High St." amounts: [10 55]) store(address: "Downtown" amounts: [4])])])} There is space for your answer on the next page.

Put your answer to the NewAddress problem here. 7

8 8. (25 points) This problem is about expressions (that encode the abstract syntax of a subset of Oz that corresponds to the one-argument λ-calculus ). Expression ::= varidexp( Atom ) funexp( Atom Expression ) applyexp( Expression Expression ) Write a function FreeVarIds: <fun {$ <Expression>}: <List <Atom>> that takes an expression Exp and returns a list of atoms from each free variable identifier occurrence in Exp. (The returned list can have duplicate elements.) An atom X should be in the result just when Exp contains a record of the form varidexp(x), and that record is not nested within a record of form funexp(x E) (since that declares X as a formal parameter). The following are examples using the Test function from the homework. Note that your answer should produce a list with the same elements, but the elements need not be in the same order as shown for the test outputs. {Test {FreeVarIds varidexp(z)} == [z]} {Test {FreeVarIds varidexp(q)} == [q]} {Test {FreeVarIds applyexp(varidexp(q) varidexp(z))} == [q z]} {Test {FreeVarIds applyexp(varidexp(x) varidexp(x))} == [x x]} {Test {FreeVarIds funexp(x varidexp(x))} == nil} {Test {FreeVarIds funexp(z applyexp(varidexp(q) varidexp(z)))} == [q]} {Test {FreeVarIds funexp(q applyexp(varidexp(q) varidexp(z)))} == [z]} {Test {FreeVarIds funexp(y applyexp(varidexp(q) varidexp(z)))} == [q z]} {Test {FreeVarIds funexp(q funexp(y applyexp(varidexp(q) varidexp(z))))} == [z]} {Test {FreeVarIds funexp(z funexp(q funexp(y applyexp(varidexp(q) varidexp(z)))))} == nil} {Test {FreeVarIds applyexp(varidexp(x) funexp(x varidexp(x)))} == [x]} {Test {FreeVarIds applyexp(funexp(x varidexp(x)) varidexp(x))} == [x]} {Test {FreeVarIds applyexp(funexp(x varidexp(x)) varidexp(y))} == [y]} {Test {FreeVarIds applyexp( applyexp( funexp(a funexp(b funexp(y applyexp(varidexp(a) varidexp(b))))) applyexp(funexp(x varidexp(x)) varidexp(y))) applyexp(varidexp(q) varidexp(z)))} == [y q z]} There is space for your answer on the next page.

Put your answer to the FreeVarIds problem here. 9