Haskell Chapter 5, Part III.

Slides:



Advertisements
Similar presentations
Haskell Chapter 5, Part I. Topics  Higher Order Functions  map, filter  Infinite lists Get out a piece of paper… we’ll be doing lots of tracing.
Advertisements

Divide. Evaluate power – 3 = – 3 EXAMPLE – 3 = 3 2 – – 3 = 6 – 3 Multiply. Evaluate expressions Multiply and divide from.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Bell Work Explain why the location of point A(1, -2) is different than the location of point B(-2, 1). **Answer in complete thought sentences.
Objective A. To multiply a polynomial by a monomial
P.2 Exponents and Scientific Notation
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
ISBN Chapter 15 Functional Programming Languages.
ORDER OF OPERATIONS x 2 Evaluate the following arithmetic expression: x 2 Each student interpreted the problem differently, resulting in.
Functional Programming in Scheme and Lisp.
Haskell Chapter 5, Part III. Recap  Lambda functions  numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100]))  What’s wrong with.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
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,
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Chapter#7 Problem Set.
Variables and Expressions
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Order of Operations Suppose we have the following:  8 = What operations are present here: Addition and Multiplication What is your result?
Polymorphic Functions
Conditional Expressions
Recursion.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Working with BODMAS Bill Haining presents.
PROGRAMMING IN HASKELL
Scientific Notation.
Higher-Order Procedures
PROGRAMMING IN HASKELL
GAM 325/425: Applied 3D Geometry
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Pattern Matching Pattern matching allows us to do things like this:
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter 1-1 Variables and expressions PreAlgebrateachers.com
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
HIGHER ORDER FUNCTIONS
PROGRAMMING IN HASKELL
Chapter 4-2 Power and Exponents
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Introduction to the Lab
Heart of Algebra Lessons 1 & 2
Lambda Expressions Cases
PROGRAMMING IN HASKELL
Multiplying Decimals and Whole Numbers
Ch 1-2 Order of Operations
Factorise b2 + 9b + 8 b2 - 9b + 8 (b + 8)(b + 1) (b - 8)(b - 1)
Algebra and Indices.
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Haskell Chapter 5, Part III

Recap What’s wrong with this? Lambda functions numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100])) What’s wrong with this? numLongChains = length (filter (\xs -> length xs > 15) ) (map chain [1..100])) Get paper out, we’ll be doing some tracing!

Function Application with $ $ is the function application operator ($) :: (a -> b) -> a -> b f $ x = f x It’s function application but: Different precedence normal function application has high precedence, $ has low precedence Different associativity normal function application is left associate, e.g., f a b c === ((f a) b) c $ is right associative *Main> (^2) 4 + 3 19 *Main> (^2) $ 4 + 3 49

Expression (e.g., map sqrt) Improved syntax with $ Most often it’s a convenience that lets us write fewer parentheses. Example: sum (map sqrt [1..130]) sum $ map sqrt [1..130] when $ is encountered, expression on right is used as parameter to function on left Same effect as parentheses! $ Function (e.g., sum) Expression (e.g., map sqrt)

More examples sqrt (3+4+9) sqrt $ 3+4+9 *Main> sum (filter (> 10) (map (*2) [2..10])) 80 *Main> sum $ filter (>10) (map (*2) [2..10]) *Main> sum $ filter (>10) $ map (*2) [2..10]

Another example *Main> (10*) $ 3 30 *Main> ($ 3) (10*) *Main> map ($ 3) [(4+), (10*), (^2), sqrt] [7.0,30.0,9.0,1.7320508075688772] How does this work? expression on right is used as parameter to function on left Trace this with your partner – add a function

Function Composition – What? In mathematics, (f o g) (x) = f(g(x)) Call g with some value, call f with the result (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) parameter of f must be the same as the return type of g Example: (negate . (*3)) 4 What’s the answer?

Function Composition – Why? Often convenient to create functions on the fly Could use lambda, but composition may be more concise *Main> map (\x -> negate (abs x)) [5,-3, -2, 7] [-5,-3,-2,-7] *Main> map (negate . abs) [5, -3, -2, 7] *Main> map (\xs -> negate (sum (tail xs))) [[1..5],[3..6],[1..7]] [-14,-15,-27] *Main> map (negate . sum . tail) [[1..5],[3..6],[1..7]] Trace this with your partner May help to do individual parts in GHCi

Function composition with multiple parameters If a function takes multiple parameters, must partially apply *Main> sum (replicate 5 (max 6 9)) 45 *Main> (sum . replicate 5) (max 6 9) *Main> sum . replicate 5 $ max 6 9 How does this syntax capture semantics? Trace this with your partner

The process To rewrite a function with lots of parentheses using function composition first write out the innermost function and its parameters then put a $ before it compose all prior functions by omitting their last parameter (but not other parameters) and putting . between them *Main> replicate 2 (product (map (*3) (zipWith max [1,2] [4,5]))) [180,180] *Main> replicate 2 . product . map (*3) $ zipWith max [1,2] [4,5] http://lambda.jstolarek.com/2012/03/function-composition-and-dollar-operator-in-haskell/

One more thought As far as I can tell, it's a stylistic choice here to select . over $? In case you were working with functions it turns out to be identical. But the $ operator is a generalization of the . operator, I mean, the dot operator (.) must be implemented with functions, but the dollar operator ($) is universal. The $ operator is implemented to avoid using parenthesis. A very simple and elegant rule to memorize its functionality sum :: (Eq a) => a -> a -> a sum a b = (a + b) versus sum2 :: (Eq a) => a -> a -> a sum2 a b = $ a + b http://codereview.stackexchange.com/questions/51032/function-composition-versus

Why have both $ and . (sidebar) Often lead to same result Biggest difference is precedence take 3 (reverse (filter even [1..10])) – goal, let’s remove () take 3 . reverse . filter even $ [1..10] – correct! take 3 . reverse . filter even [1..10] – error! take 3 . reverse . filter even partially applied, needs list take 3 . reverse . filter even [1..10] – evals filter even [1..10] SO, this is like take 3 . reverse . [2,4,6,8,10] BUT, function composition expects a function http://lambda.jstolarek.com/2012/03/function-composition-and-dollar-operator-in-haskell/

Play and Share Using $ write bigCubes that takes a list and returns a list of cubes that are > 500 write lottaBiggest that takes a list and replicates the largest element 4 times. lottaBiggest [2,5,3,1] => [5,5,5,5] write powers that takes a number and creates a list of that number squared, cubed, and quadrupled. powers 2 => [4,8,16] Assume people are dining. We have a list of tip percents (assume people tip at different rates): *Main> let pcts = [0.15, 0.2, 0.21] We have a list of bills (what people owe, minus tip) *Main> let amts = [20.5, 30, 25] Write calcBill that takes amts and pcts and calculates what each person will pay, based on their amt (* 4% tax) and pct. *Main> calcBill amts pcts [24.395,37.2,31.25] Hint: I did this first with (), then converted to remove (). I used zipWith twice and also a map (your soln may vary).

Play and Share continued Using . and/or $ write overtimePay that takes a rate a list of hours and calculates the amount of overtime pay. Same hint as above. Ex: overtimePay 20 [50,30,40] => 200 write splitBill that takes an amount, adds 25% to cover tax and tip, and splits the bill between two diners. splitBill 50 => 31.25