Lecture 9 Polymorphism, Overloading and Higher Order Functions 1 Polymorphism and Overloading.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Chapter 5: Control Structures II (Repetition)
Copyright © 2003 Pearson Education, Inc. Slide 1.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING Think Distributive property backwards Work down, Show all steps ax + ay = a(x + y)
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Functional Programming Lecture 10 - type checking.
Formal Models of Computation Part II The Logic Model
Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.
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.
Test on Input, Output, Processing, & Storage Devices
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Modern Programming Languages, 2nd ed.
ABC Technology Project
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 [3,4]
© S Haughton more than 3?
Quadratic Inequalities
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.
Twenty Questions Subject: Twenty Questions
Squares and Square Root WALK. Solve each problem REVIEW:
Energy & Green Urbanism Markku Lappalainen Aalto University.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
© 2012 National Heart Foundation of Australia. Slide 2.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
Doubles Facts Doubles with Pictures Doubles without Pictures Pictures Only.
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
1 Ke – Kitchen Elements Newport Ave. – Lot 13 Bethesda, MD.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
How Cells Obtain Energy from Food
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
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,
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Types CSCE 314 Spring 2016.
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
Functions and patterns
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
Presentation transcript:

Lecture 9 Polymorphism, Overloading and Higher Order Functions 1 Polymorphism and Overloading

Lecture 9 Polymorphism, Overloading and Higher Order Functions 2 [1,2,3,4,5,6] :: [Integer] Prelude> ['a','b','c']++['d','e','f'] "abcdef" :: [Char] Prelude> "abc"++"def" "abcdef" :: [Char] Prelude> ["abc","def","ghi"]++["uvw","xyz"] ["abc","def","ghi","uvw","xyz"] :: [[Char]] What is the type of ++ : [a] -> [a] -> [a] Prelude> [1,2,3] ++ [4,5,6]

Lecture 9 Polymorphism, Overloading and Higher Order Functions 3 lengthList (x:xs) = 1 + lengthList xs Main> lengthList ['a','b','c'] 3 :: Integer Main> lengthList [1,2,3,4,5,6] 6 :: Integer Main> lengthList [['a','b','c'],['d','e','f'],['g'],['h','i']] 4 :: Integer Main> lengthList ["abc","def"] 2 :: Integer What is the type of lengthList? [a] -> Int lengthList [ ] = 0

Lecture 9 Polymorphism, Overloading and Higher Order Functions 4 In polymorphism type variables are used, as in: [a] -> [a] -> [a] [a] -> Int (a,b) -> [a] Polymorphism: has many shapes. A function is polymorphic if it can operate on many different types. In polymorphism, the same function definition is used for all the types it is applied to. The variable a stands for an arbitrary type. Of course all the as in the first declaration above stand for the same type wheras in the third, a and b can be different types.

Lecture 9 Polymorphism, Overloading and Higher Order Functions 5 Overloading is another mechanism for using the same function name on different types. a = = b (a,b) = = (c,d) would require different definitions of = =. (a = = c) && (b = = d) An overloaded function has different definitions for different types.

Lecture 9 Polymorphism, Overloading and Higher Order Functions 6 Higher Order Functions

Lecture 9 Polymorphism, Overloading and Higher Order Functions 7 When we apply a function to all elements of a list, we say that we have mapped that function into the list. There is a Haskell built-in function called map that is used for this purpose. map f list = [ f x | x <- list] map f [ ] = [ ] map f (first:rest) = f first : map f rest Prelude> map odd [1,2,3,4,5] [True,False,True,False,True] Prelude> map (^2) [1,2,3] [1,4,9] Fact> map fact [3,4,5] [6,24,120] map: apply to all A higher order function takes a function as an argument or returns a function as a result.

Lecture 9 Polymorphism, Overloading and Higher Order Functions 8 map :: ( a -> b) -> [a] -> [b] Elements of the list to which the function is applied Elements of the output list that the function returns Type of map: input function input list output list

Lecture 9 Polymorphism, Overloading and Higher Order Functions 9 A function which doubles all the values in a list of integers: square :: Int -> Int square n = n^2 squareAll :: [Int] -> [Int] squareAll [ ] = [ ] squareAll (first : rest) = (square first) : (squareAll rest) double :: Int -> Int double n = n*2 doubleAll :: [Int] -> [Int] doubleAll [ ] = [ ] doubleAll (first:rest) = (double first) : (doubleAll rest) A function that squares all the values in a list of integers:

Lecture 9 Polymorphism, Overloading and Higher Order Functions 10 A function that returns a list of factorials of all the numbers in a given list of integers: There is a pattern that is repeated in all these definitions. Here is where we can use a higher order function. We may write a general function: doAll :: (Int -> Int) -> [Int] -> [Int] doAll f [ ] = [ ] doAll f (first : rest) = (f first ) : (doAll f rest) All we need is this one general definition and the definitions of each of the functions to be applied to each element. fact :: Int -> Int fact n = product [1..n] factAll :: [Int] -> [Int] factAll [ ] = [ ] factAll (first : rest) = (fact first) : (factAll rest)

Lecture 9 Polymorphism, Overloading and Higher Order Functions 11 double :: Int -> Int double n = n*2 square :: Int -> Int square n = n^2 fact :: Int -> Int fact n = product [1..n] doAll :: (Int -> Int) -> [Int] -> [Int] doAll f [ ] = [ ] doAll f (first : rest) = (f first ) : (doAll f rest) Main> doAll double [1,2,3,4] [2,4,6,8] Main> doAll fact [1,2,3,4] [1,2,6,24] Main> doAll square [1,2,3,4] [1,4,9,16]

Lecture 9 Polymorphism, Overloading and Higher Order Functions 12 Alternatively, we could define all the functions in one line each using our higher order function doAll: doubleAll2 :: [Int] -> [Int ] doubleAll2 list = doAll double list squareAll2 :: [Int] -> [Int ] squareAll2 list = doAll square list factAll2 :: [Int] -> [Int ] factAll2 list = doAll fact list Main> factAll2 [1,2,3,4] [1,2,6,24] Main> doubleAll2 [1,2,3,4] [2,4,6,8] Main> doAll square [1,2,3,4] [1,4,9,16]

Lecture 9 Polymorphism, Overloading and Higher Order Functions 13 We could even have used the built-in function map: doubleAll = map double where double x = 2*x Main> doAll double [1,2,3,4] [2,4,6,8] Main> map double [1,2,3,4] [2,4,6,8] Main> doAll fact [1,2,3,4] [1,2,6,24] Main> map fact [1,2,3,4] [1,2,6,24] Main> doAll square [1,2,3,4] [1,4,9,16] :: [Int] Main> map square [1,2,3,4] [1,4,9,16] Main> doubleAll [1,2,3] [2,4,6]

Lecture 9 Polymorphism, Overloading and Higher Order Functions 14 map:: (Int -> Int) -> ([Int] -> [Int]) -- -> is right associative So, we can define the following functions which return a function as their result: doubleAll3 :: [Int] -> [Int ] doubleAll3 = map double squareAll3 :: [Int] -> [Int] squareAll3 = map square factAll3 :: [Int] -> [Int] factAll3 = map fact Main> squareAll3 [1,2,3,4,5] [1,4,9,16,25] :: [Int] We can think of map as a function which takes a function of type Int -> Int and returns a function of type [Int] -> [Int] or:

Lecture 9 Polymorphism, Overloading and Higher Order Functions 15 Function remove (removes an element from a list): remove _ [ ] = [ ] remove element (first : rest) = if element = = first then remove element rest else first : (remove element rest) We may want to write a function that removes all even or odd elements or elements that satisfy a certain condition. removeOdd [ ] = [ ] removeOdd (first:rest) = if odd first then removeOdd rest else first : removeOdd rest Main> removeOdd [1,2,3,4,5,6] [2,4,6] Filtering

Lecture 9 Polymorphism, Overloading and Higher Order Functions 16 removeEven [ ]=[ ] removeEven (first : rest)= if even first then removeEven rest else first : removeEven rest We can write a general function that does all of these. removeAll p [ ] = [ ] removeAll p (first : rest) = if p first then removeAll p rest else first : removeAll p rest Main> removeEven [1,2,3,4,5,6] [1,3,5]

Lecture 9 Polymorphism, Overloading and Higher Order Functions 17 Main> removeAll odd [1,2,3,4,5,6] [2,4,6] Main> removeAll even [1,2,3,4,5,6] [1,3,5] We could have used filter to do all this rather than defining removeAll. This is another useful built-in function. It takes a property and a list and returns a list containing the elements that have that property. Main> filter odd [1,2,3,4,5,6] [1,3,5] Main> filter even [1,2,3,4,5,6,7] [2,4,6]

Lecture 9 Polymorphism, Overloading and Higher Order Functions 18 filter :: ( a -> Bool) -> [a] -> [a] The property is a function that returns a Bool Elements of the input list, the output list that the function returns and the type that the property works on Type of filter: input function input list output list

Lecture 9 Polymorphism, Overloading and Higher Order Functions 19 Definition of filter Fortunately, filter is a built-in function. If it wasnt a built-in function, we could have defined filter as follows. filter :: (Int -> Bool) -> [Int] -> [Int] filter p [ ] = [ ] filter p (first : rest) = if (p first) then first : (filter p rest) else filter p rest