CS 320 Midterm Exam. Fall 2018

Similar documents
CS 320 Midterm Exam Solution

CS 320 Homework One Due 2/5 11:59pm

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

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

CS 320: Concepts of Programming Languages

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

More fun with recursion

Shell CSCE 314 TAMU. Haskell Functions

CS 457/557: Functional Languages

Comp 311: Sample Midterm Examination

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

Exercise 1 ( = 22 points)

A general introduction to Functional Programming using Haskell

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Lecture 2: List algorithms using recursion and list comprehensions

Box-and-arrow Diagrams

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Exercise 1 ( = 24 points)

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

From the λ-calculus to Functional Programming Drew McDermott Posted

Course year Typeclasses and their instances

CSE 341 : Programming Languages Midterm, Spring 2015

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

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

CSU211 Exam 2 Fall 2007

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

CSE Programming Languages Midterm - Winter 2010

CS 320: Concepts of Programming Languages

CS 340 Spring 2019 Midterm Exam

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

An introduction introduction to functional functional programming programming using usin Haskell

Homework 1: Functional Programming, Haskell

CSE341, Fall 2011, Midterm Examination October 31, 2011

Shell CSCE 314 TAMU. Functions continued

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CS 360: Programming Languages Lecture 12: More Haskell

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

Functional Programming - 2. Higher Order Functions

CS 320: Concepts of Programming Languages

CSE341 Spring 2016, Final Examination June 6, 2016

CSE341, Fall 2011, Midterm Examination October 31, 2011

Module 8: Local and functional abstraction

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

CS3 Midterm 2 Summer 2008

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

Functional abstraction

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

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

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

Lambda Calculus and Type Inference

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

Midterm I - Solution CS164, Spring 2014

CSE341 Autumn 2017, Final Examination December 12, 2017

Haskell An Introduction

CS115 - Module 9 - filter, map, and friends

Programming Paradigms

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

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

Functional Programming in Haskell Part I : Basics

Lecture 5: Lazy Evaluation and Infinite Data Structures

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Programming Languages Fall 2013

CSE Programming Languages Final exam - Spring Answer Key

CSE341 Spring 2017, Final Examination June 8, 2017

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

CSE373 Fall 2013, Midterm Examination October 18, 2013

Introduction to Haskell

YOUR NAME PLEASE: *** SOLUTIONS ***

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

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

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

Haskell: yet another Functional Language. Distributed and Parallel Technology

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Lazy Functional Programming in Haskell

Imperative languages

Set Haskell Exercises

Background Operators (1E) Young Won Lim 7/7/18

CSE341 Spring 2017, Midterm Examination April 28, 2017

CS61A Midterm 2 Review (v1.1)

CS 209 Functional Programming

What does my program mean?

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Functional Programming. Overview. Topics. Recall λ-terms. Examples

CSE 3302 Programming Languages Lecture 8: Functional Programming

Parallel Haskell on MultiCores and Clusters

Sample Final Exam Questions

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

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

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

Introduction. chapter Functions

CS 360: Programming Languages Lecture 10: Introduction to Haskell

INTRODUCTION TO HASKELL

Working with recursion

Transcription:

Name: BU ID: CS 320 Midterm Exam Fall 2018 Write here the number of the problem you are skipping: You must complete 5 of the 6 problems on this exam for full credit. Each problem is of equal weight. Please leave blank, or draw an X through, or write Do Not Grade, on the problem you are eliminating; I will grade the first 5 I get to if I can not figure out your intention no exceptions! Use pen if possible, and try to avoid writing on the back of the page, but if you do so, please tell me! In composing your answers, remember that your goal is to show me you understand the techniques presented in the course; if you can not completely solve the problem, show me as much as you know and I will attempt to give you partial credit. Problem One (Types) Give the type for each of the following functions in the space indicated. Don t forget about class constraints if they are necessary. Use a, b, c, for type variables. (a) apply :: (a -> b) -> (c -> a) -> c -> b apply f g x = f (g x) (b) try :: Num a => a -> [a] -> [a] try x xs = x:(x+1):xs (c) mystery :: (a -> a) -> (a -> a -> a) -> a -> a -> a mystery f h x y = h (f (f x)) (h y y) (d) zipfunc :: Eq a => (a -> b) -> [a] -> [a] -> [(a, b)] zipfunc f xs ys = [ (x, f y) x <- xs, y <- ys, x /= y ]

Problem Two (Lambda Calculus) Consider the following lambda expression: (\f -> ( (\x -> f ((x x) x)) (\x -> f ((x x) x)) ) ) (z) Free! (a) Draw a line from each occurrence of a bound variable to the appropriate lambda binding. Circle any free variables and label as free. You don t have to recopy the expression, just draw on the formula as given above. (b) Give the first 2 reductions in the (infinite) sequence of reductions from this term, starting with the outermost reduction, i.e., the substitution of z for the bound variable f. (c) There is clearly a pattern here. What do you predict the term will look like after 100 reductions? Diagram this as best you can, using abbreviations---obviously you can t draw the whole thing--but convince me that you know what it will look like.

Problem Three (Let Expressions combined with Infinite Lists) In the following, a function try is defined in various ways using let expressions. For each of the following, indicate whether, in the context of that definition alone, typing (try 2) into the repl would: 1) produce a result (if so give it), 2) generate an error of some kind before even running, or 3) diverge by either running forever with no answer or trying to print out an infinite list. (a) try n = let (_,(x,y)) = ((2,3),(4,z)) (z:zs) = [1,2,3,4] (a:b:_) = [x+y,x*y,x-y] in (head zs) + a + b Solution: (1), it terminates with 11 (b) try n = let x = 3 y = x + (let n = 3 in x * n) in let n = n + 1 in n + y Solution: (3), this runs forever. (c) try n = let t = 1:[x*2 x <- t] f n = 1:[x*n x <- (f n)] in take (n+2) (f (head (tail t))) Solution: (1), it terminates with [1,2,4,8]

Problem Four (Haskell Programming) In this problem you will write a basic dictionary (map) in Haskell. The dictionary will be a list of pairs (key, value), where key and value can be arbitrary types (hence the functions you create below will be polymorphic), and where the list of pairs is sorted in ascending lexicographic order on the keys. For example, you might have the following: example :: [(Int,String)] example = [(2,"hi"), (5,"there"), (9,"folks")] (A) Write the function insert, which takes a key and a value and a dictionary, and returns a new dictionary where the pair (key,value) is now in the dictionary; if the key was already there, replace its value with the new value. Be SURE TO GIVE THE TYPE OF insert at the top of your solution. Be sure to maintain the list in ascending order of keys. (B) Write the function lookup, which takes a key and a dictionary, and if the key is in the dictionary, returns (Just value), and if not, returns Nothing. Be SURE TO GIVE THE TYPE OF lookup at the top of your solution. OR, more efficient, given the dictionary is ordered: (C) Write the function keys, which takes a dictionary and returns the list of all the keys in the table. Be SURE TO GIVE THE TYPE OF keys at the top of your solution.

Problem Five (Haskell Programming) (A) Recall that map takes a list and a function, and produces a new list by applying the function to every element of the list, having the type: map :: (a -> b)-> [a] -> [b] Implement map using the Haskell function foldr. (B) Recall that the Fibonacci numbers f0 = 1, f1 = 1, f2 = 2, f3 = 3, f4 = 5, f5 = 8 are defined by the rules f0 = f1 = 1 fn = fn-2 + fn-1 Write a definition of the infinite list of Fibonacci numbers in Haskell. Note carefully that we are NOT asking you to write a function to calculate the n th Fibonacci number, but to create an infinite list of all the Fibonacci numbers. You may use any of the standard Haskell functions. You may use list comprehensions or not, your choice. The trick here was to combine two parts of the sequence into one enumeration using zip!

Problem Six (Roll-Your-Own List Monads) Suppose you have written your own list type in Haskell, and you want to create a List Monad which behaves just as the normal one does, but using your own Lists. So for example, we should be able to do the following with our Lists, as with ordinary lists: pairs :: List a -> List b -> List (a,b) pairs xs ys = do x <- xs y <- ys return (x,y) Main> pairs (Cons 1 (Cons 2 Nil)) (Cons 3 (Cons 4 Nil)) Cons (1,3) (Cons (1,4) (Cons (2,3) (Cons (2,4) Nil))) Complete the following code template where indicated. Define any helper functions you want. import Prelude hiding ((++)) data List a = Nil Cons a (List a) deriving Show -- Just concatenation for our List type, might find this useful (++) :: List a -> List a -> List a (++) Nil ys = ys (++) (Cons x xs) ys = Cons x (xs ++ ys) instance Monad List where -- return :: a -> List a return x = Cons x Nil -- recall that (>>=) takes a List xs, applies f to each -- of its members and returns a list of all the results -- (>>=) :: List a -> (a -> List b) -> List b xs >>= f = collect xs f collect :: List a -> (a -> List b) -> List b collect Nil _ = Nil collect (Cons x xs) f = (f x) ++ (collect xs f)