Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)

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

Higher-order functions in ML
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Distributive Property
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.
Chapter 5 Section 1. Objectives 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. The Product Rule and Power Rules for Exponents Use exponents. Use.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 4 By Herb I. Gross and Richard A. Medeiros next.
The Solution of a Difference Equation for Simple Interest Account
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)
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Chapter 5 Polymorphic and Higher-Order Functions.
Creating Functions Functional Programming. The function calculator Functional programming is all about using functions Functions are first class – Take.
Multiplying, Dividing, and Simplifying Radicals
Distributive Property & Combining Like Terms
4.1 The Product Rule and Power Rules for Exponents.
In this section we will introduce a new concept which is the logarithm
Review for Final Exam Systems of Equations.
Algebra Form and Function by McCallum Connally Hughes-Hallett et al. Copyright 2010 by John Wiley & Sons. All rights reserved. 6.1 Integer Powers and the.
Chapter 5 Section 1 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Prerequisites: Fundamental Concepts of Algebra
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
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.
Changing Bases. Base 10: example number ³ 10² 10¹ 10 ⁰ ₁₀ 10³∙2 + 10²∙1 + 10¹∙ ⁰ ∙0 = 2120 ₁₀ Implied base 10 Base 8: 4110 ₈ 8³ 8².
: Adding and Subtracting Rational Expressions Introduction Expressions come in a variety of types, including rational expressions. A rational expression.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© 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.
Conditional Expressions
Recursion.
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
CHAPTER 1.3 Solving Equations.
Functions and patterns
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Chapter 5 Section 1.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Rational Exponents, Radicals, and Complex Numbers
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
Higher Order Functions
4105 CHALLENGING REVIEW QUIZ
HIGHER ORDER FUNCTIONS
Functions and patterns
PROGRAMMING IN HASKELL
Functions and patterns
Functional Programming
Presentation transcript:

Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)

Currying Recall the function: simple n a b = n*(a+b) Note that: simple n a b is really (((simple n) a) b) in fully parenthesized notation simple :: Float -> Float -> Float -> Float simple n :: Float -> Float -> Float (simple n) a :: Float -> Float ((simple n) a) b :: Float Therefore: multSumByFive a b = simple 5 a b is the same as multSumByFive = simple 5

Use of Currying listSum, listProd :: [Integer] -> Integer listSum xs = foldr (+) 0 xs listProd xs = foldr (*) 1 xs listSum = foldr (+) 0 listProd = foldr (*) 1 and, or :: [Bool] -> Bool and xs = foldr (&&) True xs or xs = foldr (||) False xs and = foldr (&&) True or = foldr (||) False

Be Careful Though... Consider: f x = g (x+2) y x This is not the same as: f = g (x+2) y because the remaining occurrence of x becomes unbound. (Or, in fact, it might be bound by some outer definition!) In general: f x = e x is the same as f = e only if x does not appear free in e.

Simplify Definitions Recall: reverse xs = foldl revOp [] xs where revOp acc x = x : acc In the prelude we have: flip f x y = f y x. (what is its type?) Thus: revOp acc x = flip (:) acc x or even better: revOp = flip (:) And thus: reverse xs = foldl (flip (:)) [] xs or even better: reverse = foldl (flip (:)) []

Anonymous Functions So far, all of our functions have been defined using an equation, such as the function succ defined by: succ x = x+1 This raises the question: Is it possible to define a value that behaves just like succ, but has no name? Much in the same way that is a value that behaves like pi ? The answer is yes, and it is written \x -> x+1. Indeed, we could rewrite the previous definition of succ as: succ = \x -> x+1.

Sections Sections are like currying for infix operators. For example: (+5) = \x -> x + 5 (4-) = \y -> 4 – y So in fact succ is just (+1) ! Note the section notation is consistent with the fact that (+), for example, is equivalent to \x -> \y -> x+y. Although convenient, however, sections are less expressive than anonymous functions. For example, it’s hard to represent \x -> (x+1)/2 as a section. You can also pattern match using an anonymous function, as in \(x:xs) -> x, which is the head function.

Function Composition Very often we would like to combine the effect of one function with that of another. Function composition accomplishes this for us, and is simply defined as the infix operator (.) : (f. g) x = f (g x) So f.g is the same as \x -> f (g x). Function composition can be used to simplify some of the previous definitions: totalSquareArea sides = sumList (map squareArea sides) = (sumList. map squareArea) sides Combining this with currying simplification yields: totalSquareArea = sumList. map squareArea