CS 457/557: Functional Languages Folds

Slides:



Advertisements
Similar presentations
Introduction 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.
Advertisements

Programming with Lists
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776(Prasad)L7fold1 Fold Operations Abstracting Repetitions.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
1 Programming Languages and Paradigms Lisp Programming.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
© M. Winter COSC 4P41 – Functional Programming Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Chapter 5 Polymorphic and Higher-Order Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Lecture 14: Advanced Topic: Functional Programming
Conditional Expressions
Recursion.
CS 550 Programming Languages Jeremy Johnson
ML: a quasi-functional language with strong typing
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher-Order Functions
From Templates to Folds
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
Lecture 15 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
Pure Kevin Edelmann.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
PROGRAMMING IN HASKELL
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Nicholas Shahan Spring 2016
Abstracting Repetitions
PROGRAMMING IN HASKELL
Functional Programming
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Higher Order Functions
PROGRAMMING IN HASKELL
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Madhusudan Parthasarathy
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
PROGRAMMING IN HASKELL
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

CS 457/557: Functional Languages Folds

Today’s topics: Folds on lists have many uses Folds capture a common pattern of computation on list values In fact, there are similar notions of fold functions on many other algebraic datatypes …)

Folds! A list xs can be built by applying the (:) and [] operators to a sequence of values: xs = x1 : x2 : x3 : x4 : … : xk : [] Suppose that we are able to replace every use of (:) with a binary operator (), and the final [] with a value n: xs = x1  x2  x3  x4  …  xk  n The resulting value is called fold () n xs Many useful functions on lists can be described in this way.

Graphically:  n e1 e2 e3 : [] e1 e2 e3 f f = foldr () n

Example: sum + e1 e2 e3 : [] e1 e2 e3 sum = foldr (+) 0

Example: product * 1 e1 e2 e3 : [] e1 e2 e3 product = foldr (*) 1

length = foldr (\x ys -> 1 + ys) 0 Example: length cons x ys = 1 + ys cons e1 e2 e3 : [] e1 e2 e3 length = foldr (\x ys -> 1 + ys) 0

map f = foldr (\x ys -> f x : ys) [] Example: map cons x ys = f x:ys cons [] e1 e2 e3 : [] e1 e2 e3 map f = foldr (\x ys -> f x : ys) []

filter p = foldr (\x ys -> if p x then x:ys else ys) [] Example: filter cons x ys = if p x then x:ys else ys cons [] e1 e2 e3 : [] e1 e2 e3 filter p = foldr (\x ys -> if p x then x:ys else ys) []

Formal Definition: foldr :: (a->b->b) -> b -> [a] -> b foldr cons nil [] = nil foldr cons nil (x:xs) = cons x (foldr cons nil xs)

Applications: sum = foldr (+) 0 product = foldr (*) 1 length = foldr (\x ys -> 1 + ys) 0 map f = foldr (\x ys -> f x : ys) [] filter p = foldr c [] where c x ys = if p x then x:ys else ys xs ++ ys = foldr (:) ys xs concat = foldr (++) [] and = foldr (&&) True or = foldr (||) False

Patterns of Computation: foldr captures a common pattern of computations over lists As such, it’s a very useful function in practice to include in the Prelude Even from a theoretical perspective, it’s very useful because it makes a deep connection between functions that might otherwise seem very different … From the perspective of lawful programming, one law about foldr can be used to reason about many other functions

A law about foldr: If () is an associative operator with unit n, then foldr () n xs  foldr () n ys = foldr () n (xs ++ ys) (x1  …  xk  n)  (y1  …  yj  n) = (x1  …  xk  y1  …  yj  n) All of the following laws are special cases: sum xs + sum ys = sum (xs ++ ys) product xs * product ys = product (xs ++ ys) concat xss ++ concat yss = concat (xss ++ yss) and xs && and ys = and (xs ++ ys) or xs || or ys = or (xs ++ ys)

foldl: There is a companion function to foldr called foldl: foldl :: (b -> a -> b) -> b -> [a] -> b foldl s n [] = n foldl s n (x:xs) = foldl s (s n x) xs For example: foldl s n [e1, e2, e3] = s (s (s n e1) e2) e3 = ((n `s` e1) `s` e2) `s` e3

foldr vs foldl: cons nil e1 e2 e3 snoc nil e3 e2 e1 foldr foldl

Uses for foldl: Many of the functions defined using foldr can be defined using foldl: sum = foldl (+) 0 product = foldl (*) 1 There are also some functions that are more easily defined using foldl: reverse = foldl (\ys x -> x:ys) [] When should you use foldr and when should you use foldl? When should you use explicit recursion instead? … (to be continued)

foldr1 and foldl1: Variants of foldr and foldl that work on non-empty lists: foldr1 :: (a -> a -> a) -> [a] -> a foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs) foldl1 :: (a -> a -> a) -> [a] -> a foldl1 f (x:xs) = foldl f x xs Notice: No case for empty list No argument to replace empty list Less general type (only one type variable)

Uses of foldl1, foldr1: From the prelude: Not in the prelude: minimum = foldl1 min maximum = foldl1 max Not in the prelude: commaSep = foldr1 (\s t -> s ++ ", " ++ t)

Example: Folds on Trees foldTree :: t -> (t -> Int -> t -> t) -> Tree -> t foldTree leaf fork Leaf = leaf foldTree leaf fork (Fork l n r) = fork (foldTree leaf fork l) n (foldTree leaf fork r) sumTree :: Tree -> Int sumTree = foldTree 0 (\l n r-> l + n + r) catTree :: Tree -> [Int] catTree = foldTree [] (\l n r -> l ++ [n] ++ r) treeSort :: [Int] -> [Int] treeSort = catTree . foldr insert Leaf