foldr and foldl applied to addition (using infix notation)

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Advertisements

Formal Models of Computation Part II The Logic Model
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.
Higher-order functions in ML
For(int i = 1; i
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
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.
Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,
© M. Winter COSC 4P41 – Functional Programming Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f.
Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Chapter 9 More About Higher-Order Functions. Currying Recall the function: simple n a b = n * (a+b) Note that: simple n a b is really (((simple n) a)
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
Chapter 5 Polymorphic and Higher-Order Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
x + 5 = 20 x+5 20 (-5) x + 5 = 20 x 20 (-5) x + 5 = 20.
12-1 Arithmetic Sequences and Series. Sequence- A function whose domain is a set of natural numbers Arithmetic sequences: a sequences in which the terms.
Com Functional Programming Higher Order Functions and Computation Patterns (I) Marian Gheorghe Lecture 10 Module homepage Mole &
Tuples and Lists Lecture 3, Programmeringsteknik del A.
Homework Questions. Number Patterns Find the next two terms, state a rule to describe the pattern. 1. 1, 3, 5, 7, 9… 2. 16, 32, 64… 3. 50, 45, 40, 35…
Инвестиционный паспорт Муниципального образования «Целинский район»
(x – 8) (x + 8) = 0 x – 8 = 0 x + 8 = x = 8 x = (x + 5) (x + 2) = 0 x + 5 = 0 x + 2 = x = - 5 x = - 2.
Homework Questions. Number Patterns Find the next two terms, state a rule to describe the pattern. 1. 1, 3, 5, 7, 9… 2. 16, 32, 64… 3. 50, 45, 40, 35…
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Lecture 2 – MapReduce: Theory and Implementation CSE 490h – Introduction to Distributed Computing, Winter 2008 Except as otherwise noted, the content of.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
Fusion Catherine Hope and Graham Hutton University of Nottingham in Less Space.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
照片档案整理 一、照片档案的含义 二、照片档案的归档范围 三、 卷内照片的分类、组卷、排序与编号 四、填写照片档案说明 五、照片档案编目及封面、备考填写 六、数码照片整理方法 七、照片档案的保管与保护.
공무원연금관리공단 광주지부 공무원대부등 공적연금 연계제도 공무원연금관리공단 광주지부. 공적연금 연계제도 국민연금과 직역연금 ( 공무원 / 사학 / 군인 / 별정우체국 ) 간의 연계가 이루어지지 않고 있 어 공적연금의 사각지대가 발생해 노후생활안정 달성 미흡 연계제도 시행전.
Arithmetic Recursive and Explicit formulas I can write explicit and recursive formulas given a sequence. Day 2.
Жюль Верн ( ). Я мальчиком мечтал, читая Жюля Верна, Что тени вымысла плоть обретут для нас; Что поплывет судно громадней «Грейт Истерна»; Что.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
© 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.
Scientific Notation.
Programming Languages and Compilers (CS 421)
How to Parallelize an Algorithm
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Lecture 15 CS 1813 – Discrete Mathematics
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Higher Order Functions
Abstracting Repetitions
Surname 99 NOTES: Point of View
Aim: What is the sequence?
Advanced Functional Programming
Pattern Matching Pattern matching allows us to do things like this:
Higher Order Functions
CS 457/557: Functional Languages Folds
CSE-321 Programming Languages Introduction to Functional Programming
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
With thanks to Nick Mooney, Spencer Pearson & Alexander Lent
PROGRAMMING IN HASKELL
Higher-Order Functions in Haskell
Timekeeper t OVER (Time lapse display Lecture Discussion
Laziness and Its Consequences
Presentation transcript:

foldr and foldl applied to addition (using infix notation) Folding foldr and foldl applied to addition (using infix notation)

First some examples Then proofs of the types of foldr and foldl

foldr (+) 0 [2,3,4] foldr (+) 0 [2,3,4] = (recursion) (+) 2 (foldr (+) 0 [3,4]) = (recursion) (+) 2 ( (+) 3 (+) (foldr (+) 0 [4]) ) = (recursion) (+) 2 ( (+) 3 ( (+) (4 (+) (foldr (+) 0 []) ))) = (base) (+) 2 ( (+) 3 ( (+) (4 (+) (0) ))) = (infix notation) This computes (2 + (3 + (4 + 0))) = 9

foldl (+) 0 [2,3,4] foldl (+) 0 [2,3,4] = (recursion) foldl (+) ((+) 0 2) [3,4] = foldl (+) (2 [3,4]) = (recursion) foldl (+) ((+) 2 3) [4]) = foldl (+) (5 [4]) = (recursion) foldl (+) ((+) 5 4) [] = (base) foldl (+) 9 [] = 9 This computes (((0(+)2)(+)3)(+)4) = 9

type of foldl / foldr As applied to (+), both functions have the type:

type of foldl / foldr As applied to (+), both functions have the type: (Int -> Int -> Int) -> (Int -> [Int] -> Int) The lecture notes show that this is a special case. In general, foldr :: (a->b->b) - > b->[a]->b foldl :: (a->b->a) -> a->[b]->a

First foldr, starting from its definition The first question to ask is, what is the type of f More specifically, what’s the type of the outcome of f? (Does this outcome have the same type as f’s 1st argument or the same type as f’s 2nd argument?)

foldr f y (x:xs) = f x (foldr f y xs) R,S,P,Q are types foldr f y [] = y P Q foldr f y (x:xs) = f x (foldr f y xs) ?? [a] a b f :: a  b  ? ?=b because Q=P=b y :: ?? ??=b because S=P Therefore, foldr :: (abb)b[a]b

Now foldl, starting from its definition foldl f y [] = y P and Q are types foldl f y (x:xs) = foldl f (fyx) xs P Q=a f :: a -> b -> ? Within fyx, the first arg of f is y, so Type(y)=a. P = Q therefore, Type(y)=P=a Therefore (comparing left and right), Type(fyx)=a. Therefore, ?=a Therefore, Type(foldl) = (a->b->a) -> a->[b]->a