CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Size: px
Start display at page:

Download "CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators"

Transcription

1 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 (or other recursive datatypes) can be defined non recursively using a folding operator. R.L. While, Modified by R. Davies

2 Patterns of recursion map and filter capture two common patterns of recursion over lists map applies a function to each element of a list filter uses a predicate to select elements from a list Both map and filter treat the elements on a list independently Both map and filter always return a list as their result filter returns a list with the same type as its argument, whereas map can return a list with a different type map returns a list with the same length as its argument, whereas filter can return a shorter list map and filter are often used together, as in list comprehensions But most functions don t return a list as their result! Another common (and even more general) pattern of recursion is exhibited by functions like sum, product, or, and, length, concat, insertsort, etc even map and filter themselves! CITS3211 Functional Programming 2 6. Folding operators

3 foldr foldr :: (a > b > b) > b > [a] > b foldr f b [] = b foldr f b (x : xs) = f x (foldr f b xs) fold from the right foldr op b [] = b foldr op b [x1] = op x1 b = x1 `op` b foldr op b [x1, x2] = op x1 (op x2 b) = x1 `op` (x2 `op` b) foldr op b [x1, x2, x3] = op x1 (op x2 (op x3 b)) = x1 `op` (x2 `op` (x3 `op` b)) etc CITS3211 Functional Programming 3 6. Folding operators

4 foldl foldl :: (b > a > b) > b > [a] > b foldl f b [] foldl f b (x : xs) = b = foldl f (f b x) xs fold from the left foldl op b [] = b foldl op b [x1] = op b x1 = b `op` x1 foldl op b [x1, x2] = op (op b x1) x2 = (b `op` x1) `op` x2 foldl op b [x1, x2, x3] = op (op (op b x1) x2) x3 = ((b `op` x1) `op` x2) `op` x3 etc CITS3211 Functional Programming 4 6. Folding operators

5 Packaged recursion sum = foldr (+) 0 product = foldr (*) 1 or = foldr ( ) False and = foldr (&&) True length = foldr (const (+ 1)) 0 concat = foldr (++) [] insertsort = foldr insert [] where insert x xs =... map f = foldr ((:). f) [] filter p = foldr f [] where f x b p x = x : b not (p x) = b CITS3211 Functional Programming 5 6. Folding operators

6 foldr1 and foldl1 These two functions assume that the argument list is not empty note the more restricted types foldr1 :: (a > a > a) > [a] > a foldr1 f [x] = x foldr1 f (x : xs) = f x (foldr1 f xs) foldr1 f [] = error foldr1 has an empty list foldl1 :: (a > a > a) > [a] > a foldl1 f (x : xs) = foldl f x xs foldl1 f [] = error foldl1 has an empty list e.g. maximum, minimum, unwords CITS3211 Functional Programming 6 6. Folding operators

7 Higher order functions over non list datatypes Higher order functions can be defined over any recursive datatype Consider the type Bintree data Bintree a = Empty Leaf a Node a (Bintree a) (Bintree a) Mapping a function over a Bintree involves applying a function to each element of type a mapt :: (a > b) > Bintree a > Bintree b mapt f Empty = Empty mapt f (Leaf x) = Leaf (f x) mapt f (Node x l r) = Node (f x) (mapt f l) (mapt f r) e.g. mapplus, takefst Folding a Bintree with a function involves replacing each terminal with a value of the result type and folding up each internal node foldt :: b > (a > b) > (a > b > b > b) > Bintree a > b foldt b f g Empty = b foldt b f g (Leaf x) = f x foldt b f g (Node x l r) = g x (foldt b f g l) (foldt b f g r) e.g. size, frontier mapt f = foldt Empty (Leaf. f) (Node. f) CITS3211 Functional Programming 7 6. Folding operators

8 mapt and foldt The structure of these definitions corresponds closely to the structure of the datatype (as described in the data declaration) There is always one equation for each constructor foldt has one argument for each constructor (plus the tree itself) the type of each argument is determined by the type of the corresponding constructor mapt has one argument for each constructor with non recursive sub parts (plus the tree itself) the type of each argument is determined by the types of the sub parts of the corresponding constructor cf foldr and map CITS3211 Functional Programming 8 6. Folding operators

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

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

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

Functional Programming - 2. Higher Order Functions

Functional Programming - 2. Higher Order Functions Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

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

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

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

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

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

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

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

(add1 3) 4 (check-expect (add1 3) 4)

(add1 3) 4 (check-expect (add1 3) 4) (add1 3) 4 (check-expect (add1 3) 4) (define T 7) (define (q z) (sqr z)) (cond [(> T 3) (q 4)] [else 9]) (cond [(> T 3) (q 4)] [else 9]) -->[const] ^ (cond [(> 7 3) (q 4)] [else 9]) -->[arith] ^^^^^^^

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

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees Module 9: Trees If you have not already, make sure you Read How to Design Programs Sections 14, 15, 16. 1 CS 115 Module 9: Trees Mathematical Expressions We are going to discuss how to represent mathematical

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

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

Programming Languages 3. Definition and Proof by Induction

Programming Languages 3. Definition and Proof by Induction Programming Languages 3. Definition and Proof by Induction Shin-Cheng Mu Oct. 22, 2015 Total Functional Programming The next few lectures concerns inductive definitions and proofs of datatypes and programs.

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

CSE 130 [Winter 2014] Programming Languages

CSE 130 [Winter 2014] Programming Languages CSE 130 [Winter 2014] Programming Languages Higher-Order Functions! Ravi Chugh! Jan 23 Today s Plan A little more practice with recursion Base Pattern Base Expression Induction Pattern Induction Expression

More information

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4]

Write: evens. evens [] ====> [] evens [1;2;3;4] ====> [2;4] Write: evens (* val evens: int list -> int list *) let rec evens xs = [] ->... x::xs ->... evens [] ====> [] evens [1;2;3;4] ====> [2;4] Write: evens (* val evens: int list -> int list *) let rec evens

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

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

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

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

CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

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

CITS3211 FUNCTIONAL PROGRAMMING. 13. Interpretation

CITS3211 FUNCTIONAL PROGRAMMING. 13. Interpretation CITS3211 FUNCTIONAL PROGRAMMING 13. Interpretation Summary: This lecture discusses the construction of a simple interpreter for Haskell. R.L. While, 1997 2002, R. Davies 2003 7 Interpretation The simplest

More information

Foundations of Computation

Foundations of Computation The Australian National University Semester 2, 2018 Research School of Computer Science Tutorial 5 Dirk Pattinson Foundations of Computation The tutorial contains a number of exercises designed for the

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

CS115 - Module 10 - General Trees

CS115 - Module 10 - General Trees Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Sections 15 and 16. Arithmetic Expressions Recall with binary trees we could represent an expression containing binary

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages From Trees to Type Classes Mark P Jones Portland State University 1 Trees:! " There are many kinds of tree data structure.! " For example: data BinTree a = Leaf a BinTree

More information

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong. Data Abstraction An Abstraction for Inductive Data Types Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Introduction This lecture

More information

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University Talen en Compilers 2017-2018, period 2 Jurriaan Hage Department of Information and Computing Sciences Utrecht University November 13, 2017 1. Introduction 1-1 This lecture Introduction Course overview

More information

Introduction to Functional Programming

Introduction to Functional Programming A Level Computer Science Introduction to Functional Programming William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London Aims and Claims Flavour of Functional

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

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

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions CSE 130 : Programming Languages Higher-Order Functions Ranjit Jhala UC San Diego Recursion A way of life A different way to view computation Solutions for bigger problems From solutions for sub-problems

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

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

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING Monday 15 th December 2014 14:30 to 16:30 INSTRUCTIONS TO CANDIDATES 1.

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

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =

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

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

Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya 04-05-2001, 09.00-12.00, Educatorium Gamma This test consists of two parts. For the first part, which is

More information

Lecture 21: Functional Programming in Python. List Comprehensions

Lecture 21: Functional Programming in Python. List Comprehensions The University of North Carolina at Chapel Hill Spring 2002 Lecture 21: Functional Programming in March 1 1 List Comprehensions Haskell Lists can be defined by enumeration using list comprehensions Syntax:

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

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

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

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList

More information

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

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2. Watch out for the arrows Recollecting Haskell, Part V Higher Types CIS 352/Spring 2018 Programming Languages January 30, 2018 1 / 28 2 / 28 A start on higher types: Mapping, 1 Mapping via list comprehension

More information

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

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18) Advanced Programming Handout 7 Monads and Friends (SOE Chapter 18) The Type of a Type In previous chapters we discussed: Monomorphic types such as Int, Bool, etc. Polymorphic types such as [a], Tree a,

More information

Haskell Assignment 3 This assignment is worth 50 points.

Haskell Assignment 3 This assignment is worth 50 points. Haskell Assignment 3 This assignment is worth 50 points. Notes: You must write type signatures for all functions/values you define. If you re ever confused about what the best type signature for a function

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-16: Haskell,

More information

Tree Oriented Programming. Jeroen Fokker

Tree Oriented Programming. Jeroen Fokker Tree Oriented Programming Jeroen Fokker Tree oriented programming Many problems are like: Input text transform process unparse Output text Tree oriented programming Many problems are like: Input text parse

More information

Lecture: Functional Programming

Lecture: Functional Programming Lecture: Functional Programming This course is an introduction to the mathematical foundations of programming languages and the implementation of programming languages and language-based tools. We use

More information

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO Exam in INF3110, November 29, 2017 Page 1 UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Exam in: INF3110 Programming Languages Day of exam: November 29, 2017 Exam hours: 14:30 18:30

More information

Introduction to Programming: Lecture 6

Introduction to Programming: Lecture 6 Introduction to Programming: Lecture 6 K Narayan Kumar Chennai Mathematical Institute http://www.cmi.ac.in/~kumar 28 August 2012 Example: initial segments Write a Haskell function initsegs which returns

More information

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter (CTM Sections 1.9, 3.6, 4.7) Carlos Varela RPI

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

CS115 - Module 8 - Binary trees

CS115 - Module 8 - Binary trees Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Section 14. Binary arithmetic expressions Operators such as +,,, and take two arguments, so we call them binary operators.

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

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

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create

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

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

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 4 Lists and Recursion Don Sannella University of Edinburgh Part I Lists and Recursion Cons and append Cons takes an element and a list. Append takes two lists.

More information

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10 Oct 04, 16 21:55 Page 1/10 * Lecture 02 Infer some type arguments automatically. Set Implicit Arguments. Note that the type constructor for functions (arrow " >") associates to the right: A > B > C = A

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information

CITS3211 FUNCTIONAL PROGRAMMING

CITS3211 FUNCTIONAL PROGRAMMING CITS3211 FUNCTIONAL PROGRAMMING 9. The λ calculus Summary: This lecture introduces the λ calculus. The λ calculus is the theoretical model underlying the semantics and implementation of functional programming

More information

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

From Templates to Folds

From Templates to Folds From Templates to Folds CS 5010 Program Design Paradigms Bootcamp Lesson 6.3 Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

More information

Inductive Data Types

Inductive Data Types Inductive Data Types Lars-Henrik Eriksson Functional Programming 1 Original slides by Tjark Weber Lars-Henrik Eriksson (UU) Inductive Data Types 1 / 42 Inductive Data Types Today Today New names for old

More information

Exercise 1 (2+2+2 points)

Exercise 1 (2+2+2 points) 1 Exercise 1 (2+2+2 points) The following data structure represents binary trees only containing values in the inner nodes: data Tree a = Leaf Node (Tree a) a (Tree a) 1 Consider the tree t of integers

More information

Warm-up and Memoization

Warm-up and Memoization CSE341 Spring 05 Due Wednesday, May 11 Assignment 4 Part I Warm-up and Memoization Warm-up As a warm-up, write the following Scheme functions: 1. Write the function foldl of the form (foldl func initial

More information

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

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

The Typed Racket Guide

The Typed Racket Guide The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

More information

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen,

More information

Functional programming Primer I

Functional programming Primer I Functional programming Primer I COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Characteristics of functional programming Primary notions: functions and expressions (not

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

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

An introduction introduction to functional functional programming programming using usin Haskell

An introduction introduction to functional functional programming programming using usin Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program

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

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

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

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor. Trees 1. Consider the following data type Tree and consider an example inhabitant tree: data Tree a = Bin (Tree a) a (Tree a) Leaf deriving Show tree :: Tree Int tree = Bin (Bin (Bin Leaf 1 Leaf ) 2 (Bin

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

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp http://www.cs.washington.edu/341/ Lists (list expr2... exprn) '(value1 value2...

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

Synthesizing Data Structure Transformations from Input-Output Examples

Synthesizing Data Structure Transformations from Input-Output Examples Synthesizing Data Structure Transformations from Input-Output Examples John K. Feser Rice University, USA feser@rice.edu Swarat Chaudhuri Rice University, USA swarat@rice.edu Isil Dillig UT Austin, USA

More information

Exercise 1 ( = 18 points)

Exercise 1 ( = 18 points) 1 Exercise 1 (4 + 5 + 4 + 5 = 18 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a = Leaf

More information

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.

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. Oct 07, 16 18:11 Page 1/10 * Lecture 02 Set Implicit Arguments. Inductive list (A: Type) : Type := nil : list A cons : A > list A > list A. Fixpoint length (A: Type) (l: list A) : nat := nil _ => O cons

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

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING Friday 20 th December 2013 14:30 to 16:30 INSTRUCTIONS TO CANDIDATES 1.

More information

Typed Scheme: Scheme with Static Types

Typed Scheme: Scheme with Static Types Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit

More information

Sum-of-Product Data Types

Sum-of-Product Data Types CS251 Programming Languages Handout # 22 Prof. Lyn Turbak February 2, 2005 Wellesley College 1 Introduction Sum-of-Product Data Types Every general-purpose programming language must allow the processing

More information

Shell CSCE 314 TAMU. Haskell Functions

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

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

The Algebra of Programming in Haskell

The Algebra of Programming in Haskell The Algebra of Programming in Haskell Bruno Oliveira The Algebra of Programming in Haskell p.1/21 Datatype Generic Programming - Motivation/Goals The project is to develop a novel mechanism for parameterizing

More information