Special Directions for this Test



Similar documents
The Clean programming language. Group 25, Jingui Li, Daren Tuzi


Chapter 15 Functional Programming Languages

J a v a Quiz (Unit 3, Test 0 Practice)

3. Mathematical Induction

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Project 2: Bejeweled

Part I. Multiple Choice Questions (2 points each):

Anatomy of Programming Languages. William R. Cook

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Sample CSE8A midterm Multiple Choice (circle one)

Example of a Java program

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Math 55: Discrete Mathematics

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

(Eng. Hayam Reda Seireg) Sheet Java

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

The Habit Programming Language: The Revised Preliminary Report

Euler Paths and Euler Circuits

Visual Basic Programming. An Introduction

Practical Generic Programming with OCaml

6.3 FACTORING ax 2 bx c WITH a 1

CISC 181 Project 3 Designing Classes for Bank Accounts

Bridgemate developer s guide

Fun with Phantom Types

Introduction to Python

FLORIDA STATE COLLEGE AT JACKSONVILLE COLLEGE CREDIT COURSE OUTLINE. Introduction to Programming with Visual Basic.NET

SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University

Overview of CheckMarket question types

Grandstream XML Application Guide Three XML Applications

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

A Concrete Introduction. to the Abstract Concepts. of Integers and Algebra using Algebra Tiles

Portal Connector Fields and Widgets Technical Documentation

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

BAR CODE CONTROL BAR CODE CONTROL - 1

CSc 372. Comparative Programming Languages. 21 : Haskell Accumulative Recursion. Department of Computer Science University of Arizona

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Exercise 4 Learning Python language fundamentals

Factoring and Applications

Changing the Display Frequency During Scanning Within an ImageControls 3 Application

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Java Classes. GEEN163 Introduction to Computer Programming

Selection Statements

CSCE 314 Programming Languages

Pseudo code Tutorial and Exercises Teacher s Version

Formal Engineering for Industrial Software Development

CS 111 Classes I 1. Software Organization View to this point:

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

Rigorous Software Development CSCI-GA

Programming Database lectures for mathema

by Mario Fusco Monadic

FACTORING OUT COMMON FACTORS

The if Statement and Practice Problems

When a variable is assigned as a Process Initialization variable its value is provided at the beginning of the process.

User guide for the Error & Warning LabVIEW toolset

Chicago Booth BUSINESS STATISTICS Final Exam Fall 2011

Global Variables. However, when global variables are used in a function block or control modules, they must be declared as external

Objective-C and Cocoa User Guide and Reference Manual. Version 5.0

5.3 Graphing Cubic Functions

Conditionals (with solutions)

Data Structures and Algorithms Written Examination

Programming and Reasoning with Side-Effects in IDRIS

F ahrenheit = 9 Celsius + 32

The Cool Reference Manual

3 length + 23 size = 2

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Java Basics: Data Types, Variables, and Loops

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Compsci 101: Test 1. October 2, 2013

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Grade 7/8 Math Circles Fall 2012 Factors and Primes

Regression Verification: Status Report

WebSphere Business Monitor

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Party Time! Computer Science I. Signature Examples PAIR OBJ. The Signature of a Procedure. Building a Set. Testing for Membership in a Set

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

PL/JSON Reference Guide (version 1.0.4)

Paper Merges and Joins Timothy J Harrington, Trilogy Consulting Corporation

Moving from CS 61A Scheme to CS 61B Java

Programming Project 1: Lexical Analyzer (Scanner)

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Microsoft Access 3: Understanding and Creating Queries

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

Excel Database Management

Single Page Web App Generator (SPWAG)

ML for the Working Programmer

Lecture 12: Abstract data types

ESPResSo Summer School 2012

Intel Retail Client Manager Audience Analytics

Intermediate Code. Intermediate Code Generation

Transcription:

1 Spring, 2013 Name: (Please do not write your id number!) COP 4020 Programming Languages I Test on Haskell and Functional Programming Special Directions for this Test This test has 7 questions and pages numbered 1 through 8. This test is open book and notes, but no electronics. 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. Take special care with indentation and capitalization in Haskell. When you write Haskell code on this test, you may use anything we have mentioned in class that is built-in to Haskell. But unless specifically directed, you should not use imperative features (such as the IO type). You are encouraged to define functions not specifically asked for if they are useful to your programming; however, if they are not in the standard Haskell Prelude, then you must write them into your test. (That is, your code may not import modules other than the Prelude.) Hints If you use functions like filter, map, and foldr whenever possible, then you will have to write less code on the test, which will mean fewer chances for making mistakes and will leave you more time to be careful. The problem will note explicitly if you are prohibited from using such functions, but by default you can. In the follow the grammar problems the examples may be very extensive and take a long time to read if you read every detail. But the basic idea of the recursion is usually clear from the description, simple examples, and the follow the grammar idea of recursing everywhere possible. So look to the examples to confirm your understanding and don t spend too much time reading examples after you understand the problem. For Grading Question: 1 2 3 4 5 6 7 Total Points: 10 5 10 15 15 20 25 100 Score:

2 1. (10 points) [UseModels] In Haskell, write the function: squareevens :: [Integer] -> [Integer] that takes a list of Integers, lst, and returns a list of Integers that is just like lst, except that each even element of lst is replaced by the square of that element. In your solution, you might find it helpful to use the built-in predicate even. The following are examples, written using the Testing module from the homework. tests :: [TestCase [Integer]] tests = [eqtest (squareevens []) "==" [],eqtest (squareevens [3]) "==" [3],eqTest (squareevens [4]) "==" [16],eqTest (squareevens [4,3]) "==" [16,3],eqTest (squareevens [1,2,3,4,5,6]) "==" [1,4,3,16,5,36],eqTest (squareevens [3,22,3,95,600,0,-2]) "==" [3,484,3,95,360000,0,4]

3 2. (5 points) [Concepts] [UseModels] Consider the data type Amount defined below. data Amount = Zero One Two In Haskell, write the polymorphic function rotate :: Amount -> (a,a,a) -> (a,a,a) which takes an Amount, amt, and a triple of elements of some type, (x,y,z), and returns a triple that is circularly rotated to the right by the number of steps indicated by the English word that corresponds to amt. That is, when amt is Zero, then (x,y,z) is returned unchanged; when amt is One, then (z,x,y) is returned; finally, when amt is Two, then (y,z,x) is returned. The following are examples, written using the Testing module from the homework. tests :: [TestCase Bool] tests = [asserttrue ((rotate Zero (1,2,3)) == (1,2,3)),assertTrue ((rotate One (1,2,3)) == (3,1,2)),assertTrue ((rotate Two (1,2,3)) == (2,3,1)),assertTrue ((rotate Two ("jan","feb","mar")) == ("feb","mar","jan")),asserttrue ((rotate One ("jan","feb","mar")) == ("mar","jan","feb")),asserttrue ((rotate Zero (True,False,True)) == (True,False,True)) ]

4 3. (10 points) [UseModels] Consider the type of address book entries below. data Entry = Record {name :: String, phone :: Integer, email :: String} Write, in Haskell, the function nophones :: [Entry] -> [(String,String)] that takes a list of records of type Entry, es, and returns a list of pairs of the name and email address of each entry, in the same order as in es. The following are examples, written using the Testing module from the homework. tests :: [TestCase [(String,String)]] tests = [eqtest (nophones []) "==" [],eqtest (nophones [Record {name = "Eastman", phone = 3214442211, email = "kodak@k.com"}]) "==" [("Eastman","kodak@k.com")],eqTest (nophones [Record {name = "M", phone = 44153543221, email = "m@mi6.uk"},record {name = "Bond", phone = 44007007007, email ="jb@mi6.uk"}]) "==" [("M","m@mi6.uk"),("Bond","jb@mi6.uk")],eqTest (nophones sample) "==" [("Adams","adams@mail.com"),("Bullfinch","bf@bf.com"),("Cassidy","cass@mail.com"),("Durham","bull@dingers.com"),("Eastman","kodak@k.com")],eqTest (nophones ((Record {name="durham",phone=3059123344, email="crash@yahoo.com"}):sample)) "==" [("Durham","crash@yahoo.com"),("Adams","adams@mail.com"),("Bullfinch","bf@bf.com"),("Cassidy","cass@mail.com"),("Durham","bull@dingers.com"),("Eastman","kodak@k.com")] ] where sample = [Record {name = "Adams", phone = 4075551212, email = "adams@mail.com"},record {name = "Bullfinch", phone = 5155551212, email = "bf@bf.com"},record {name = "Cassidy", phone = 8005551122, email = "cass@mail.com"},record {name = "Durham", phone = 3059123344, email = "bull@dingers.com"},record {name = "Eastman", phone = 3214442211, email = "kodak@k.com"}]

5 4. (15 points) [Concepts] [UseModels] Without using any functions from the Haskell prelude, write the polymorphic function unpickedmap :: (a -> Bool) -> (a -> a) -> [a] -> [a] which takes a predicate, p, a function f, and a list lst, and returns a list that is just like lst, but in which every element x that does not satisfy p is replaced by f applied to x. (An element x satisfies p if (p x) == True.) The following are examples, written using the Testing module from the homework. tests :: [TestCase Bool] tests = [asserttrue ((unpickedmap odd (\x -> x*x) []) == []),asserttrue ((unpickedmap odd (\x -> 66) [2,4,6,8,10]) == [66,66,66,66,66]),assertTrue ((unpickedmap odd (\x -> x*x) [1,2,3,4,5]) == [1,4,3,16,5]),assertTrue ((unpickedmap even (\x -> 6000) [3,1,-5]) == [6000,6000,6000]),assertTrue ((unpickedmap (\c -> c == 'q') (\x -> 'u') "quip") == "quuu"),asserttrue ((unpickedmap (== False) not [True,False,True]) == [False,False,False]) ]

6 5. (15 points) [Concepts] [UseModels] Without using any functions from the Haskell prelude, and without using a list comprehension, write the polymorphic function partition :: (a -> Bool) -> [a] -> ([a],[a]) which takes a predicate, p, and a list lst, and returns a pair of lists (no,yes) such that no contains the elements of lst that do not satisfy p, and yes contains the elements of lst that satisfy p. In both no and yes the order of elements is the same as that in lst. The following are examples, written using the Testing module from the homework. tests :: [TestCase Bool] tests = [asserttrue ((partition odd []) == ([],[])),asserttrue ((partition odd [1..10]) == ([2,4,6,8,10],[1,3,5,7,9])),assertTrue ((partition even [1..10]) == ([1,3,5,7,9],[2,4,6,8,10])),assertTrue ((partition (== 3) [1..5]) == ([1,2,4,5],[3])),assertTrue ((partition (== 3) [5,7,2]) == ([5,7,2],[])),assertTrue ((partition (== 3) [3,3,3]) == ([],[3,3,3])),assertTrue ((partition (== 3) [3,3,4,3]) == ([4],[3,3,3]))

7 6. (20 points) [UseModels] This problem is about the type WindowLayout, which is defined as follows. data WindowLayout = Window {wname :: String, width :: Int, height :: Int} In Haskell, write a function Horizontal [WindowLayout] Vertical [WindowLayout] iconify :: WindowLayout -> WindowLayout that takes a WindowLayout, wl, and returns a WindowLayout that is just like wl, except that in each Window record, the value of each width and height field is replaced by 2. The following are examples using the Testing module from the homework. tests :: [TestCase WindowLayout] tests = [eqtest (iconify Window {wname="castle", width=1280, height=740}) "==" (Window {wname="castle", width=2, height=2}),eqtest (iconify (Horizontal [Window {wname="castle", width=1280, height=740}, Window {wname="bball", width=900, height=900}])) "==" (Horizontal [Window {wname="castle", width=2, height=2}, Window {wname="bball", width=2, height=2}]),eqtest (iconify (Vertical [])) "==" (Vertical []),eqtest (iconify (Horizontal [])) "==" (Horizontal []),eqtest (iconify (Vertical [Horizontal [Window {wname="castle", width=1280, height=740}, Window {wname="bball", width=900, height=900}], Vertical [Window {wname="csi", width=1000, height=500}]])) "==" (Vertical [Horizontal [Window {wname="castle", width=2, height=2}, Window {wname="bball", width=2, height=2}], Vertical [Window {wname="csi", width=2, height=2}]]),eqtest (iconify (Horizontal [Vertical [Window {wname="csi", width=1280, height=740}, Window {wname="daily", width=900, height=900}], Horizontal [Window {wname="news", width=1000, height=500}, "==" (Horizontal [Vertical [Window {wname="csi", width=2, height=2}, Be sure to follow the grammar! Horizontal [Window {wname="pbs", width=800,height=400}]]])) Window {wname="daily", width=2, height=2}], Horizontal [Window {wname="news", width=2, height=2}, Horizontal [Window {wname="pbs", width=2,height=2}]]]) ]

8 7. (25 points) [UseModels] Consider the data type of quantified Boolean expressions defined as follows. data QBExp = Varref String QBExp `Or` QBExp Exists String QBExp Your task is to write a function freeqbexp :: QBExp -> [String] that takes a QBExp, qbe, and returns a list containing just the strings that occur as a free variable reference in qbe. The following defines what occurs as a free variable reference means. A string s occurs as a variable reference in a QBExp if s appears in a subexpression of the form (Varref s). Such a string occurs as a free variable reference if it occurs as a variable reference in a subexpression that is outside of any expression of the form (Exists s e), which declares s. The following are examples that use the course s Testing module. Note that the lists returned by freeqbexp should have no duplicates. In the tests, the seteq function constructs a test case that considers lists of strings to be equal if they have the same elements (so that the order is not important). tests :: [TestCase [String]] tests = [seteq (freeqbexp (Varref "x")) "==" ["x"],seteq (freeqbexp ((Varref "x") `Or` (Varref "y"))) "==" ["x","y"],seteq (freeqbexp ((Varref "y") `Or` (Varref "x"))) "==" ["y","x"],seteq (freeqbexp (((Varref "y") `Or` (Varref "x")) "==" ["y","x"] `Or` ((Varref "x") `Or` (Varref "y")))),seteq (freeqbexp (Exists "y" (Varref "y"))) "==" [],seteq (freeqbexp (Exists "y" ((Varref "y") `Or` (Varref "z")))) "==" ["z"],seteq (freeqbexp (Exists "z" (Exists "y" ((Varref "y") `Or` (Varref "z"))))) "==" [],seteq (freeqbexp ((Varref "z") "==" ["z"] `Or` (Exists "z" (Exists "y" ((Varref "y") `Or` (Varref "z")))))),seteq (freeqbexp (((Varref "z") `Or` (Varref "q")) `Or` (Exists "z" (Exists "y" ((Varref "y") `Or` (Varref "z")))))) "==" ["z","q"] ] where seteq = gtest setequal setequal los1 los2 = (length los1) == (length los2) && subseteq los1 los2 subseteq los1 los2 = all (\e -> e `elem` los2) los1