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

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

Standard prelude. Appendix A. A.1 Classes

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.

A general introduction to Functional Programming using Haskell

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Functional Programming TDA 452, DIT 142

Lecture 4: Higher Order Functions

Lecture 10 September 11, 2017

Functional Programming TDA 452, DIT 142

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

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

1 The smallest free number

Haskell Types COMP360

Abstract Types, Algebraic Types, and Type Classes

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

Programming Languages 3. Definition and Proof by Induction

A tour of the Haskell Prelude

CS 320: Concepts of Programming Languages

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

INTRODUCTION TO HASKELL

Course year Typeclasses and their instances

301AA - Advanced Programming

Higher Order Functions in Haskell

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

CS 457/557: Functional Languages

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Lecture 2: List algorithms using recursion and list comprehensions

CS 320: Concepts of Programming Languages

Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

Functional Programming and Haskell

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

Introduction to Programming: Lecture 6

Lazy Functional Programming in Haskell

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Logic - CM0845 Introduction to Haskell

Haskell through HUGS THE BASICS

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Introduction to Functional Programming in Haskell 1 / 56

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

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

HIGHER-ORDER FUNCTIONS

CSC 372 Haskell Mid-term Exam Feburary 28, 2014

Overloading, Type Classes, and Algebraic Datatypes

CSE 3302 Programming Languages Lecture 8: Functional Programming

Advanced features of Functional Programming (Haskell)

Programming Paradigms Written Exam (6 CPs)

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

Haskell An Introduction

Lecture 5: Lazy Evaluation and Infinite Data Structures

An introduction introduction to functional functional programming programming using usin Haskell

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

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

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Programming Languages Fall 2013

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

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

CPSC 311, 2010W1 Midterm Exam #2

Deriving Generic Functions by Example

CS 320 Midterm Exam. Fall 2018

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

Questions & Answers 28 Sept.

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

CS 440: Programming Languages and Translators, Spring 2019 Mon

Principles of Programming Languages

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

Shell CSCE 314 TAMU. Functions continued

Homework 3 COSE212, Fall 2018

CIS 252 Introduction to Computer Science Section M013: Exam 2 (Green) March 29, Points Possible

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Programming Paradigms Written Exam

Lecture 19: Functions, Types and Data Structures in Haskell

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Haskell Types, Classes, and Functions, Currying, and Polymorphism

INDEX. Symbols & Numbers. Learn You a Haskell for Great Good! 2011 by Miran Lipovača

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Theorem Proving Principles, Techniques, Applications Recursion

CS115 - Module 10 - General Trees

Haskell: From Basic to Advanced. Haskell buzzwords. Hello, World! History. Haskell 98 / Haskell 2010 GHC

Logical Methods in... using Haskell Getting Started

What s in Main. Tobias Nipkow. December 5, Abstract

Logic Informatics 1 Functional Programming: Tutorial 6

CS 275 Name Final Exam Solutions December 16, 2016

Introduction to Programming, Aug-Dec 2006

CSCE 314 Programming Languages

LECTURE 16. Functional Programming

Transcription:

EXAM FUNCTIONAL PROGRAMMING Tuesday the 1st of October 2016, 08.30 h. - 10.30 h. Name: Student number: Before you begin: Do not forget to write down your name and student number above. If necessary, explain your answers (in English or Dutch). For multiple choice questions, clearly circle what you think is the (one and only) best answer. Use the empty boxes under the other questions to write your answer and explanations in. Use the blank paper provided with this exam only as scratch paper (kladpapier). At the end of the exam, only hand in the filled-in exam paper. Answers will not only be judged for correctness, but also for clarity and conciseness. A total of 100 points can be obtained. Good luck! In any of your answers below you may (but do not have to) use the following well-known Haskell functions/operators, unless stated otherwise: replicate, id, concat, foldr (and variants), map, filter, const, all, any, flip, fst, snd, not, (.), elem, take, drop, takewhile, dropwhile, head, tail, zip, reverse, (++), lookup, max, min and all members of the type classes Eq, Num, Ord, Show and Read. 1. LISTS (i) Define a function vouw :: [a ] > [(a, a)] that takes a list xs, and pairs the first with the last, the second with the one-but-last, and so on. If the list has odd length, the middle element is paired with a copy of itself. So vouw [1, 2, 3, 4, 5] = [(1, 5), (2, 4), (3, 3)] and vouw [1, 2, 3, 4, 5, 6] = [(1, 6), (2, 5), (3, 4)]. Implement this function without using recursion yourself, and without list comprehensions.... /8 (ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs. ulh [1, 2, 3, 2] = [(1, 2), (1, 3), (1, 2), (2, 1), (2, 3), (2, 2), (3, 1), (3, 2), (3, 2), (2, 1), (2, 2), (2, 3)], in that order. Do this using recursion and without list comprehensions. Moreover, of the above listed functions you may only use (++). You may define your own local definitions.... /9 1

(iii) Would (ii) be easy to do with a list comprehension? If so, explain how; if not, explain what makes it hard.... /4 2. DATATYPES Consider the following simplified datatype for representing boolean expressions (propositions), where variable names consist of a single character: data Prop = Cons Bool Vari Char Not Prop Prop : : Prop Here, the constructor : : represents conjunction, and the constructor Not represents the negation symbol. (i) Give the value of type Prop that represents the proposition ( v w) tt where v and w represent variables, and tt represents the value true.... /6 (ii) Write an evaluator eval :: (Char > Bool) > Prop > Bool that takes a function that maps variables to booleans, and a proposition, and returns the boolean value of that proposition.... /10 2

(iii) For all propositions p, ( p) = p. Write a simplifier simpl :: Prop > Prop that uses this (and only this) equality as much as it can to simplify boolean propositions. For example, simpl prop = Cons True : : Not (Vari v ) where prop = Not (Not (Not (Not (Cons True)) : : Not (Vari v ))).... /8 (iv) We also have some equalities for conjunction ( ): p ff = ff = ff p and p tt = p = true p. Now, simpl prop = Not (Vari v ) for the prop given in (iii). Extend simpl to apply also these optimisations.... /6 3

3. SUBLISTS In this question we deal with a function subs :: [a ] > [[a ]] which returns all the sublists of the argument list (i.e., all the lists that result by deleting elements from the argument list in any possible way). (i) How many sublists does [1,2,3,4] have?... /4 (ii) Explain how you can compute subs (x :xs) from subs xs (for example by using concrete values for x and xs).... /4 (iii) Now, write the function subs :: [a ] > [[a ]] exploiting sharing where you can.... /6 4

4. MULTIPLE CHOICE... /20 The following multiple choice questions are each worth 5 points. (i) (ii) I For a left-associative operator, the expression a b c should be interpreted as a (b c). II When we say infix :<>: in Haskell, we mean that the operator :<>: is associative, so that we can write expressions like a :<>: b :<>: c. a. Both I and II are true b. Only I is true c. Only II is true d. Both I and II are false I [const 2, (flip const) "2"], where flip flips the first two arguments of a function, is well-typed. II x > [(flip. flip) x, id ] is well-typed. a. Both I and II are true b. Only I is true c. Only II is true d. Both I and II are false (iii) What is the type of map ( } :). map reverse. map ( { :)? a. [[a ]] > [[a ]] b. [[String ]] > [[String ]] c. [[Char ]] > [[Char ]] d. The expression is type incorrect. (iv) The function intersperse :: a > [a ] > [a ] puts its first argument between all the elements of a non-empty list. Thus intersperse a "xyz" results in "xayaz". Which definition is correct, assuming the argument as is not empty? a. intersperse a as = foldr ( e r > (e : a : r)) [ ] as b. intersperse a as = foldl ( r e > (a : e : r)) [ ] as c. intersperse a as = (tail. concat. map ( x > [a, x ])) as d. intersperse a as = tail [(a : e) e < as ] 5

5. TYPE INFERENCE Determine the type of foldr concatmap, where concatmap :: (a > [b ]) > [a ] > [b ]. You should not just write down the type below, but also explain how you arrived at that type (for example, in the way that this is done in the lecture notes of this course).... /15 6