CSCE 314 Programming Languages

Similar documents
Shell CSCE 314 TAMU. Higher Order Functions

Standard prelude. Appendix A. A.1 Classes

Shell CSCE 314 TAMU. Functions continued

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

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

CSCE 314 Programming Languages. Functional Parsers

CSCE 314 Programming Languages. Monadic Parsing

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Programming Languages 3. Definition and Proof by Induction

Lecture 4: Higher Order Functions

Lecture 10 September 11, 2017

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

CSCE 314 Programming Languages. Monadic Parsing

CS 340 Spring 2019 Midterm Exam

Exercise 1 ( = 24 points)

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

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

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

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

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Shell CSCE 314 TAMU. Haskell Functions

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

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

Logic - CM0845 Introduction to Haskell

The Haskell HOP: Higher-order Programming

CSCE 314 Programming Languages Functors, Applicatives, and Monads

Functional Programming in Haskell for A level teachers

CS 457/557: Functional Languages

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

CSCE 314 Programming Languages. Interactive Programming: I/O

CSCE 314 Programming Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

Week 5 Tutorial Structural Induction

Introduction to Programming, Aug-Dec 2006

Higher Order Functions in Haskell

INTRODUCTION TO HASKELL

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

A general introduction to Functional Programming using Haskell

CS 320: Concepts of Programming Languages

Functional Programming - 2. Higher Order Functions

Exercise 1 ( = 18 points)

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

Introduction to Functional Programming

List Functions, and Higher-Order Functions

A tour of the Haskell Prelude

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

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

Lecture 21: Functional Programming in Python. List Comprehensions

CS 440: Programming Languages and Translators, Spring 2019 Mon

Continuation Passing Style. Continuation Passing Style

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

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

HIGHER-ORDER FUNCTIONS

Haskell Refresher Informatics 2D

Functional Programming and Haskell

Higher-Order Functions

Lecture 5: Lazy Evaluation and Infinite Data Structures

Using Strategies for Assessment of Functional Programming Exercises

Topic 5: Examples and higher-order functions

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CSE 3302 Programming Languages Lecture 8: Functional Programming

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.

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

Programming Paradigms Written Exam

Foundations of Computation

CSCE 314 Programming Languages

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Efficient Mergesort. Christian Sternagel. August 28, 2014

Exercise 1 (2+2+2 points)

Lecture 8: Summary of Haskell course + Type Level Programming

FUNCTIONAL PEARLS The countdown problem

Lecture 2: List algorithms using recursion and list comprehensions

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CSE399: Advanced Programming. Handout 2

Programming Language Concepts: Lecture 14

Lecture 19: Functions, Types and Data Structures in Haskell

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

Faster Haskell. Neil Mitchell

Lambda expressions, functions and binding

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

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Programming in Haskell Aug-Nov 2015

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

301AA - Advanced Programming

Introduction to Programming: Lecture 6

CSCE 314 Programming Languages

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

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

Functional Programming in Haskell Part I : Basics

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

List are immutable Lists have recursive structure Lists are homogeneous

1 The smallest free number

Transcription:

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 a result. twice :: (a a) a a twice f x f (f x) twice is higher-order because it takes a function as its first argument. Note: Higher-order functions are very common in Haskell (and in functional programming). Writing higher-order functions is crucial practice for effective programming in Haskell, and for understanding others code. 2

Why Are They Useful? Common programming idioms can be encoded as functions within the language itself. Domain specific languages can be defined as collections of higher-order functions. For example, higher-order functions for processing lists. Algebraic properties of higher-order functions can be used to reason about programs. 3

The Map Function The higher-order library function called map applies a function to every element of a list. map :: (a b) [a] [b] For example: > map (+1) [1,3,5,7] [2,4,6,8] The map function can be defined in a particularly simple manner using a list comprehension: map f xs [f x x xs] Alternatively, it can also be defined using recursion: map f [] [] map f (x:xs) f x : map f xs Lee CSCE 314 TAMU 4

The Filter Function The higher-order library function filter selects every element from a list that satisfies a predicate. filter :: (a Bool) [a] [a] For example: > filter even [1..10] [2,4,6,8,10] Filter can be defined using a list comprehension: filter p xs [x x xs, p x] Alternatively, it can be defined using recursion: filter p [] [] filter p (x:xs) p x x : filter p xs otherwise filter p xs 5

The foldr Function A number of functions on lists can be defined using the following simple pattern of recursion: f [] v f (x:xs) x f xs f maps the empty list to some value v, and any non-empty list to some function applied to its head and f of its tail. 6

For example: sum [] 0 sum (x:xs) x + sum xs v 0 + product [] 1 product (x:xs) x * product xs v 1 * and [] True and (x:xs) x && and xs v True && 7

The higher-order library function foldr (fold right) encapsulates this simple pattern of recursion, with the function and the value v as arguments. For example: sum foldr (+) 0 product foldr (*) 1 or and foldr ( ) False foldr (&&) True 8

foldr itself can be defined using recursion: foldr :: (a b b) b [a] b foldr f v [] v foldr f v (x:xs) f x (foldr f v xs) However, it is best to think of foldr nonrecursively, as simultaneously replacing each (:) in a list by a given function, and [] by a given value. 9

For example: sum [1,2,3] foldr (+) 0 [1,2,3] foldr (+) 0 (1:(2:(3:[]))) 1+(2+(3+0)) 6 Replace each (:) by (+) and [] by 0. 10

For example: product [1,2,3] foldr (*) 1 [1,2,3] foldr (*) 1 (1:(2:(3:[]))) 1*(2*(3*1)) 6 Replace each (:) by (*) and [] by 1. 11

Other foldr Examples Even though foldr encapsulates a simple pattern of recursion, it can be used to define many more functions than might first be expected. Recall the length function: length length [] 0 :: [a] Int length (_:xs) 1 + length xs 12

For example: length [1,2,3] length (1:(2:(3:[]))) 1+(1+(1+0)) 3 Replace each (:) by λ_ n 1+n and [] by 0 Hence, we have: length foldr (\_ n -> 1+n) 0 13

Now the reverse function: reverse [] [] reverse (x:xs) reverse xs ++ [x] For example: reverse [1,2,3] reverse (1:(2:(3:[]))) (([] ++ [3]) ++ [2]) ++ [1] [3,2,1] Hence, we have: Replace each (:) by λx xs xs ++ [x] and [] by [] reverse foldr (\x xs -> xs ++ [x]) [] Here, the append function (++) has a particularly compact definition using foldr: Replace each (:) by (++ ys) foldr (:) ys (:) and [] by ys. 14

Why Is foldr Useful? Some recursive functions on lists, such as sum, are simpler to define using foldr. Properties of functions defined using foldr can be proved using algebraic properties of foldr. Advanced program optimizations can be simpler if foldr is used in place of explicit recursion. 15

foldr and foldl foldr :: (a b b) b [a] b foldr f v [] v foldr f v (x:xs) f x (foldr f v xs) foldl :: (a b a) a [b] a foldl f v [] v foldl f v (x:xs) foldl f (f v x) xs foldr 1 : 2 : 3 : [] > (1 + (2 + (3 + 0))) foldl 1 : 2 : 3 : [] > (((0 + 1) + 2) + 3) 16

Other Library Functions The library function (.) returns the composition of two functions as a single function. (.) :: (b -> c) -> (a -> b) -> (a -> c) f. g \x -> f (g x) For example: odd :: Int Bool odd not. even Exercise: Define filterout p xs that retains elements that do not satisfy p. filterout p xs filter (not. p) xs > filterout odd [1..10] [2,4,6,8,10] 17

The library function all decides if every element of a list satisfies a given predicate. all :: (a Bool) [a] Bool all p xs and [p x x xs] For example: > all even [2,4,6,8,10] True 18

Dually, the library function any decides if at least one element of a list satisfies a predicate. any :: (a Bool) [a] Bool any p xs or [p x x xs] For example: > any isspace "abc def" True 19

The library function takewhile selects elements from a list while a predicate holds of all the elements. takewhile :: (a Bool) [a] [a] takewhile p [] [] takewhile p (x:xs) p x x : takewhile p xs otherwise [] For example: > takewhile isalpha "abc def" "abc" 20

Dually, the function dropwhile removes elements while a predicate holds of all the elements. dropwhile :: (a Bool) [a] [a] dropwhile p [] [] dropwhile p (x:xs) p x dropwhile p xs otherwise x:xs For example: > dropwhile isspace " abc" "abc" 21

filter, map and foldr Typical use is to select certain elements, and then perform a mapping, for example, sumsquaresofpos ls foldr (+) 0 (map (^2) (filter (> 0) ls)) > sumsquaresofpos [-4,1,3,-8,10] 110 In pieces: keeppos filter (> 0) mapsquare map (^2) sum foldr (+) 0 sumsquaresofpos ls sum (mapsquare (keeppos ls)) Alternative definition: sumsquaresofpos sum. mapsquare. keeppos 22