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

Size: px
Start display at page:

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

Transcription

1 Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya , , Educatorium Gamma This test consists of two parts. For the first part, which is a multiple choice, you'll get 3 points for each good answer; 0 point if you don't answer; and -1 point if your answer is wrong. Don't forget to write your name and student number. This test is open book. You're allowed to have your lecture notes with you, but no other document.

2 Part I [max. 30 pt] 1) A function f is defined as : f = filter null. head What is the (most general) type of f? a) [[a]]->[[a]] b) [[[a]]]->[a] c) [[[a]]]->[[a]] d) Eq a => [[[a]]]->[[a]] 2) Which of the following functions is not a correct alternate definition for the function f from the question No. 1? a) f s = (.) (filter null) head s b) f s = (filter null). head s c) f = (\s-> filter null (head s)) d) f = (.) (filter null) head 3) The priority and associativity of a number of operators are defined as follows: infixr 5 : infix 4 `elem` Which of the following expressions is equal to:? (a:) `elem` b : c a) a : `elem` b : c b) a `elem` (b:c) c) elem (a:b:c) d) elem (a:) (b:c) 4) Given a list s of tuples, the function sym produces a list of all x s such that (x,x) is an element of s. Example: sym [(2,3),(3,3),(0,0),(4,0)] = [3,0] Which of the following definitions is a correct definition of sym?

3 a) sym = map fst. filter p p (x,y) = x==y b) sym s = map fst (filter (x==y) s) c) sym s = map fst (filter (fst == snd) s) d) sym = map fst. filter (==) 5) A function f is defined as: f b c = [] `elem` b c What is the (most general) type of f? a) Eq [a] => (a ->[[a]] ) -> a -> Bool b) Eq a => (b -> [[a]]) -> b -> Bool c) Eq a => (b -> [a] ) -> b -> Bool d) Eq a, Eq b => (b -> [a]) -> b -> Bool 6) Given a lijst m::[[int]], which of the following functions computes the sum of all prime numbers in m. You may assume that the function isprime::int->bool, which tests whether a number is prime, is already defined. a) sumprime m = (sum. filter isprime) m b) sumprime m = (sum. map sum. filter isprime) m c) sumprime m = (sum. map sum. map (filter isprime)) m d) sumprime m = map (sum. filter isprime) m 7) A function f is defined as : f s = foldr op 0 s op x r = head x + r Which of the following functions is a correct alternate definition of f? a) f s = sum (map head s) b) f s = map op s op x r = head x + r c) f [] = 0 f (x:s) = head x + r d) None of the above answers is correct

4 8) Given a list m::[[a]], which function counts the number of empty lists in m? a) cntempty m = foldr (==[]) 0 m b) cntempty m = foldr (\x r-> if null x then 1 else 0) 0 m c) cntempty m = foldr op 0 m op x r = if null x = 1 + cntempty r else cntempty r d) cntempty m = foldr op 0 m op x r = if null x then 1+r else r 9) The type Tree is defined as : data Tree = Red [Tree] Blue [Tree] Which of the functions below is well defined and returns a value of type Tree? a) f Nil = Nil f (Red s) = Red (map f s) f (Blue s) = Blue (map f s) b) f (Red s) = Blue (map Red s) f (Blue s) = Red (map Blue s) c) f [] = Nil f (x:s) = Blue x (f s) d) f s = Blue (map Red s) 10) A type Tree and a function f are defined as follows: data Tree a = Node a [Tree a] f (Node x ts) = 1 + maximum' (map f ts) The function maximum' is a variant of maximum that also works on [] werkt. The maximum' of [] is defined as 0. So: maximum' [] = 0 maximum' s = maximum s Which of the following statements is true? a) f t computes the height of the tree t b) f t computes the number of elements in t c) We can also define f like this: f (Node x ts) = foldr maximum' 1 ts d) The most general type of f is Ord a => Tree a -> Int

5 Part II [max. 60 pt] 1. [30 pt] The pircture below is a simplified version of the road map of the netherland. The circles are cities and the arrows are the roads between the cities. The number above or beside (printed in bold) an arrow is the distance of the road represented by the arrow. To make it simple, we assume the road is one way, and the direction is indicated by the direction of the arrow. Two-ways road can be represented by two one-wat roads. Denhaag (4) 40 Utrecht (0) 50 Arnhem (1) Breda (3) Eindhoven (2) We are going to represent a road map with a list of roads. A road is a tuple (x,y,a); x and y are the two cities connected by the road. The direction of the road is from x to y, and the distance is a. Cities are represented by integers rather than strings. So: type City = Int type Distance = Int type Road = (City,City,Distance) type Map = [Road] As an example, the map above is represented by the following list: [(4,0,40),(0,1,50),(4,3,50),(3,2,40),(0,2,80)]

6 Always write the type of your functions! If you don't or if your typing is wrong, you'll get less points. a) [8 pt] Given a map m and a city s, write a function inmap to test whether the city s occurs in m. You have to write this function using fold (foldr or foldl or their variants). b) [8 pt] Given a map m and a city s, write a function nbr that retrurns all neighboring cities of s in a list. A city t is a neighbor of another city s if there is a road that goes from s to t in m. For example, Utrecht is a neighbour of Denhaag, but Arnhem is not a neighbour of Denhaag. Write this function as a composition of map and filter. c) [5 pt] Given a map m and a number n, write a function that tests whether there exists a city in m which is unreachable. Een city is unreachable if it has no incoming road. For example, in the map from the previous page, Denhaag is an unreachable city. You may assume that n is the number of the cities in m, and that the cities are numbered from 0 up to n-1. Hint: you may want to use the function nub. You get the maximum points if your solution is as simple and as efficient as possible. d) [9 pt] A path through a map can be represented with a non-empty list of roads. For example: [(4,3,50),(3,2,40)] represents the path from Denhaag to Eindhoven via Breda in the map from the previous page. Given the map m, the function generate generates a list of all paths in m. So the type of this function is: generate :: Map -> [[Road]] You may assume that this function exists. The paths generated by this fucntion are really the paths of the input map? so they are not arbitrary lists of roads. Given a map m, two cities s en t, and an int n, write a function pathexists to test whether there is a path from s to t whose total distance is less than n. Make as much use as possible of higher order functions such as map and filter. Give an argument why your function terminates. You may also use the following functions: from :: [Road]->City from ((x,_,_):_) = x to :: [Road]->City to path = y (_,y,_) = last path So, given a path p from x to y, then we have from p = x and to p = y.

7 2. [30 pt] A book is a hierarchical structure. A book consists of book parts. Book parts can be chapters, sections, sub-sections, sub-subsections, and so on. A book itself is a book part. A paragraph is also a book part. Every book part except paragraph consists of zero or more sub-parts. A paragraph has no sub-part, but it has content, namely a piece of text. In addition we also want to let all book parts except paragraphs to carry some additional information. This additional info can be a number or a title. To keep it general we will represent the type of this extra info with a type variable a. a) [5 pt] Give a representation of books as a type in Haskell. b) [10 pt] Two books b and c are equal if: (1) they have the same extra information, (2) they have equal number of sub-parts, en (3) for all i, the i th sub-part of b is equal to the i th sub-part of c. If b and c are paragraphs, then they are equal if their contents are equal. Write an instance declaration that declares that the type you give in a) is an instance of the class Eq. The instance must implement the above notion of equality. c) [15 pt] Write a function labelmetpartel that given a book b (a value of the type you give in a)) does the following. The function replaces the extra information of every sub-part d in b with a number n which is the number of all parapgrahs in d. You should count all paragraphs, including those that are deeper in the hirarchy. The replace should also be applied on every sub-part in b, including those parts which are deeper in the hierarchy. The function labelmetpartel should be a recursive function that walk through the 'tree' only once? so it contains no nested recursion. A solition with nested recursion is in this case very inefficient; if you give this inefficient solution you'll only get maximum 5 points. Don't forget to write the type of your functions.

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

Functional Programming Mid-term exam Tuesday 3/10/2017 Functional Programming Mid-term exam Tuesday 3/10/2017 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.

More information

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

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs. 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

More information

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

Haskell Overview II (2A) Young Won Lim 8/9/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 440: Programming Languages and Translators, Spring 2019 Mon Haskell, Part 4 CS 440: Programming Languages and Translators, Spring 2019 Mon 2019-01-28 More Haskell Review definition by cases Chapter 6: Higher-order functions Revisit currying map, filter Unnamed

More information

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

Haskell Types, Classes, and Functions, Currying, and Polymorphism 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the

More information

A general introduction to Functional Programming using Haskell

A general introduction to Functional Programming using Haskell A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell

More information

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

Haskell Overview II (2A) Young Won Lim 8/23/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee 1 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as

More information

Standard prelude. Appendix A. A.1 Classes

Standard prelude. Appendix A. A.1 Classes Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from

More information

Shell CSCE 314 TAMU. Higher Order Functions

Shell CSCE 314 TAMU. Higher Order Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Higher Order Functions 2 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as a result.

More information

INTRODUCTION TO HASKELL

INTRODUCTION TO HASKELL INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

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

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 7 Map, filter, fold Don Sannella University of Edinburgh Part I Map Squares *Main> squares [1,-2,3] [1,4,9] squares :: [Int] -> [Int] squares xs [ x*x x

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

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

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 11 Data Representation Don Sannella University of Edinburgh Part I Complexity t = n vs t = n 2 10.4 9.6 8.8 8 7.2 6.4 5.6 4.8 4 3.2 2.4 1.6 0.8 0 0.8 1.6 2.4

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

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

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

More information

Higher Order Functions in Haskell

Higher Order Functions in Haskell Higher Order Functions in Haskell Evan Misshula 2018-09-10 Outline Curried Functions Curried comparison Example partial application partial application of a string function Returned functions ZipWith flip

More information

CS 340 Spring 2019 Midterm Exam

CS 340 Spring 2019 Midterm Exam CS 340 Spring 2019 Midterm Exam Instructions: This exam is closed-book, closed-notes. Electronic devices of any kind are not permitted. Write your final answers, tidily, in the boxes provided. Scratch

More information

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators CITS3211 FUNCTIONAL PROGRAMMING 6. Folding operators Summary: This lecture discusses an important group of higher order functions known as the folding operators. Almost any recursive function over lists

More information

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe! Lecture 7: Haskell CSC 131 Fall, 2014 Kim Bruce According to Larry Wall (designer of PERL): a language by geniuses for geniuses He s wrong at least about the latter part though you might agree when we

More information

Lecture 4: Higher Order Functions

Lecture 4: Higher Order Functions Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function

More information

Higher-Order Functions

Higher-Order Functions Higher-Order Functions Tjark Weber Functional Programming 1 Based on notes by Pierre Flener, Jean-Noël Monette, Sven-Olof Nyström Tjark Weber (UU) Higher-Order Functions 1 / 1 Tail Recursion http://xkcd.com/1270/

More information

Logic - CM0845 Introduction to Haskell

Logic - CM0845 Introduction to Haskell Logic - CM0845 Introduction to Haskell Diego Alejandro Montoya-Zapata EAFIT University Semester 2016-1 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester

More information

Logical Methods in... using Haskell Getting Started

Logical Methods in... using Haskell Getting Started Logical Methods in... using Haskell Getting Started Jan van Eijck May 4, 2005 Abstract The purpose of this course is to teach a bit of functional programming and logic, and to connect logical reasoning

More information

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

CMSC 330: Organization of Programming Languages. Functional Programming with Lists CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as

More information

301AA - Advanced Programming

301AA - Advanced Programming 301AA - Advanced Programming Lecturer: Andrea Corradini andrea@di.unipi.it h;p://pages.di.unipi.it/corradini/ Course pages: h;p://pages.di.unipi.it/corradini/dida@ca/ap-18/ AP-2018-19: Algebraic Datatypes

More information

Haskell Overview III (3A) Young Won Lim 10/4/16

Haskell Overview III (3A) Young Won Lim 10/4/16 (3A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

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

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1 A Third Look At ML Chapter Nine Modern Programming Languages, 2nd ed. 1 Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order

More information

Haskell through HUGS THE BASICS

Haskell through HUGS THE BASICS Haskell through HUGS THE BASICS FP for DB Basic HUGS 1 Algorithmic Imperative Languages variables assignment if condition then action1 else action2 loop block while condition do action repeat action until

More information

Advanced features of Functional Programming (Haskell)

Advanced features of Functional Programming (Haskell) Advanced features of Functional Programming (Haskell) Polymorphism and overloading January 10, 2017 Monomorphic and polymorphic types A (data) type specifies a set of values. Examples: Bool: the type of

More information

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

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 12 Data Abstraction Don Sannella University of Edinburgh Part I Sets as lists without abstraction We will look again at our four ways of implementing sets.

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Exercises on ML. Programming Languages. Chanseok Oh

Exercises on ML. Programming Languages. Chanseok Oh Exercises on ML Programming Languages Chanseok Oh chanseok@cs.nyu.edu Dejected by an arcane type error? - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (fn x=> fn y => fn z => (max

More information

CSc 372 Comparative Programming Languages

CSc 372 Comparative Programming Languages CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

More information

CSC 372 Haskell Mid-term Exam Feburary 28, 2014

CSC 372 Haskell Mid-term Exam Feburary 28, 2014 Last name, NetID CSC 372 Haskell Mid-term Exam Feburary 28, 2014 READ THIS FIRST Read this page now but do not turn this page until you are told to do so. Go ahead and fill in your last name and NetID

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

More information

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona 1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists

More information

User-Defined Algebraic Data Types

User-Defined Algebraic Data Types 72 Static Semantics User-Defined Types User-Defined Algebraic Data Types An algebraic data type declaration has the general form: data cx T α 1... α k = K 1 τ 11... τ 1k1... K n τ n1... τ nkn introduces

More information

Programming Language Concepts, CS2104 Lecture 7

Programming Language Concepts, CS2104 Lecture 7 Reminder of Last Lecture Programming Language Concepts, CS2104 Lecture 7 Tupled Recursion Exceptions Types, ADT, Haskell, Components 5th Oct 2007 CS2104, Lecture 7 1 Overview 5th Oct 2007 CS2104, Lecture

More information

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

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

CMSC 330: Organization of Programming Languages. Functional Programming with Lists CMSC 330: Organization of Programming Languages Functional Programming with Lists 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as a linked data structure

More information

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

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

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.

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. EDAF40 2nd June 2017 14: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. DO NOT WRITE WITH OTHER COLOUR THAN BLACK - coloured text

More information

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists CITS3211 FUNCTIONAL PROGRAMMING 7. Lazy evaluation and infinite lists Summary: This lecture introduces lazy evaluation and infinite lists in functional languages. cs123 notes: Lecture 19 R.L. While, 1997

More information

The Haskell HOP: Higher-order Programming

The Haskell HOP: Higher-order Programming The Haskell HOP: Higher-order Programming COS 441 Slides 6 Slide content credits: Ranjit Jhala, UCSD Agenda Haskell so far: First-order functions This time: Higher-order functions: Functions as data, arguments

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,

More information

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE 3302 Programming Languages Lecture 8: Functional Programming CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages

More information

F28PL1 Programming Languages. Lecture 14: Standard ML 4

F28PL1 Programming Languages. Lecture 14: Standard ML 4 F28PL1 Programming Languages Lecture 14: Standard ML 4 Polymorphic list operations length of list base case: [] ==> 0 recursion case: (h::t) => 1 more than length of t - fun length [] = 0 length (_::t)

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 06: Useful Haskell Syntax, HO Programming Continued o Goodbye to Bare Bones Haskell: Built-in

More information

Lecture 10 September 11, 2017

Lecture 10 September 11, 2017 Programming in Haskell S P Suresh http://www.cmi.ac.in/~spsuresh Lecture 10 September 11, 2017 Combining elements sumlist :: [Int] -> Int sumlist [] = 0 sumlist (x:xs) = x + (sumlist xs) multlist :: [Int]

More information

Algebraic Types. Chapter 14 of Thompson

Algebraic Types. Chapter 14 of Thompson Algebraic Types Chapter 14 of Thompson Types so far Base types Int, Integer, Float, Bool, Char Composite types: tuples (t 1,t 2,,t n ) lists [t 1 ] functions (t 1 -> t 2 ) Algebraic types enumerated, product

More information

Haskell Refresher Informatics 2D

Haskell Refresher Informatics 2D Haskell Purely functional! : Everything is a function Haskell Refresher Informatics 2D Kobby. K.A. Nuamah 30 January 2015 Main topics: Recursion Currying Higher-order functions List processing functions

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

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

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems General Computer Science (CH08-320101) Fall 2016 SML Tutorial: Practice Problems November 30, 2016 Abstract This document accompanies the traditional SML tutorial in GenCS. It contains a sequence of simple

More information

Box-and-arrow Diagrams

Box-and-arrow Diagrams Box-and-arrow Diagrams 1. Draw box-and-arrow diagrams for each of the following statements. What needs to be copied, and what can be referenced with a pointer? (define a ((squid octopus) jelly sandwich))

More information

Week 5 Tutorial Structural Induction

Week 5 Tutorial Structural Induction Department of Computer Science, Australian National University COMP2600 / COMP6260 Formal Methods in Software Engineering Semester 2, 2016 Week 5 Tutorial Structural Induction You should hand in attempts

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Haskell An Introduction

Haskell An Introduction Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

Programming Languages Fall 2013

Programming Languages Fall 2013 Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

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

Haskell Overview II (2A) Young Won Lim 9/26/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

CS 11 Haskell track: lecture 5. This week: State monads

CS 11 Haskell track: lecture 5. This week: State monads CS 11 Haskell track: lecture 5 This week: State monads Reference "Monads for the Working Haskell Programmer" http://www.engr.mun.ca/~theo/misc/ haskell_and_monads.htm Good explanation of state monads Today's

More information

Type-indexed functions in Generic Haskell

Type-indexed functions in Generic Haskell Type-indexed functions in Generic Haskell Johan Jeuring September 15, 2004 Introduction Today I will talk about: a Types of polymorphism. b Type-indexed functions. c Dependencies. Read about b, and c in

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,

More information

INTRODUCTION TO FUNCTIONAL PROGRAMMING

INTRODUCTION TO FUNCTIONAL PROGRAMMING INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham adapted by Gordon Uszkay 1 What is Functional Programming? Opinions differ, and it is difficult to give a precise definition,

More information

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists Plan Chapter 3 Linear Structures: Lists 1. Lists... 3.2 2. Basic operations... 3.4 3. Constructors and pattern matching... 3.5 4. Polymorphism... 3.8 5. Simple operations on lists... 3.11 6. Application:

More information

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

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

More information

Lazy Functional Programming in Haskell

Lazy Functional Programming in Haskell Lazy Functional Programming in Haskell David Raymond Christiansen 25 November, 2013 What is Haskell? 2 What is Haskell? Pure functional language: no side effects 2 What is Haskell? Pure functional language:

More information

CSE413 Midterm. Question Max Points Total 100

CSE413 Midterm. Question Max Points Total 100 CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,

More information

A tour of the Haskell Prelude

A tour of the Haskell Prelude A tour of the Haskell Prelude Bernie Pope 2001 1 Haskell The Haskell language was conceived during a meeting held at the 1987 Functional Programming and Computer Architecture conference (FPCA 87). At the

More information

CSci 4223 Principles of Programming Languages

CSci 4223 Principles of Programming Languages CSci 4223 Principles of Programming Languages Lecture 11 Review Features learned: functions, tuples, lists, let expressions, options, records, datatypes, case expressions, type synonyms, pattern matching,

More information

CSE399: Advanced Programming. Handout 2

CSE399: Advanced Programming. Handout 2 CSE399: Advanced Programming Handout 2 Higher-Order Programming Functions as Data In Haskell (and other functional languages), functions can be treated as ordinary data they can be passed as arguments

More information

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists Plan Chapter 3 Linear Structures: Lists 1. Two constructors for lists... 3.2 2. Lists... 3.3 3. Basic operations... 3.5 4. Constructors and pattern matching... 3.6 5. Simple operations on lists... 3.9

More information

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 5: Lazy Evaluation and Infinite Data Structures Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a

More information

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams. Font of all knowledge? Australian researchers develop typeface they say can boost memory, that could help students cramming for exams. About 400 university students took part in a study that found a small

More information

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7. Homework 5 Due Wednesday, 14 March Handout 19 CSCI 334: Spring 2018 Notes This homework has two types of problems: Problems: You will turn in answers to these questions. Pair Programming: This part involves

More information

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona 1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/40 The Hugs Interpreter The

More information

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

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona CSc 372 Comparative Programming Languages 11 : Haskell Higher-Order Functions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2010 Christian Collberg Higher-Order Functions

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 9 January 31, 2018 Lists and Higher-order functions Lecture notes: Chapter 9 What is the type of this expresssion? [ (fun (x:int) -> x + 1); (fun (x:int)

More information

Functional Programming TDA 452, DIT 142

Functional Programming TDA 452, DIT 142 Chalmers Göteborgs Universitet 2016-04-07 Examiner: David Sands dave@chalmers.se. Answering questions on the day of the exam (at approx 15.00): Gregoire Detrez (tel: 073 55 69 550) and at other times by

More information

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

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions Higher-Order Functions CSc 520 Principles of Programming Languages 16: Haskell Higher-Order Functions Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08025 INFORMATICS 1 - INTRODUCTION TO COMPUTATION

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08025 INFORMATICS 1 - INTRODUCTION TO COMPUTATION UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08025 INFORMATICS 1 - INTRODUCTION TO COMPUTATION Thursday 13 th December 2018 14:30 to 16:30 INSTRUCTIONS TO CANDIDATES

More information

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

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November 2013 Type Classes Don Sannella University of Edinburgh Mock exam Slots and rooms have now been assigned Mon 18

More information

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 372 Comparative Programming Languages. 4 : Haskell Basics CSc 372 Comparative Programming Languages 4 : Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg August 23, 2011

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Functional Programming in Haskell for A level teachers

Functional Programming in Haskell for A level teachers Functional Programming in Haskell for A level teachers About this document Functional Programming is now part of the A level curriculum. This document is intended to get those who already have some programming

More information

More fun with recursion

More fun with recursion Informatics 1 Functional Programming Lecture 6 More fun with recursion Don Sannella University of Edinburgh Part I Counting Counting Prelude [1..3] [1,2,3] Prelude enumfromto 1 3 [1,2,3] [m..n] stands

More information

Abstract Types, Algebraic Types, and Type Classes

Abstract Types, Algebraic Types, and Type Classes Informatics 1 Functional Programming Lectures 13 and 14 Monday 9 and Tuesday 10 November 2009 Abstract Types, Algebraic Types, and Type Classes Philip Wadler University of Edinburgh Reminders Tutorial

More information

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

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Datatype declarations

Datatype declarations Datatype declarations datatype suit = HEARTS DIAMONDS CLUBS SPADES datatype a list = nil (* copy me NOT! *) op :: of a * a list datatype a heap = EHEAP HEAP of a * a heap * a heap type suit val HEARTS

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

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

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness Haskell: From Basic to Advanced Part 3 A Deeper Look into Laziness Haskell is a lazy language Laziness again A particular function argument is only evaluated when it is needed, and if it is needed then

More information

Course year Typeclasses and their instances

Course year Typeclasses and their instances Course year 2016-2017 Typeclasses and their instances Doaitse Swierstra and Atze Dijkstra with extra s Utrecht University September 29, 2016 1. The basics 2 Overloading versus parametric polymorphism 1

More information