First Class Anonymous Functions
Introduction to Higher Order Functions
- Dr. Mattox Beckman
Introduction to Higher Order Functions Dr. Mattox Beckman - - PowerPoint PPT Presentation
First Class Anonymous Functions Introduction to Higher Order Functions Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science First Class Anonymous Functions Objectives Explain the concept of fjrst
First Class Anonymous Functions
First Class Anonymous Functions
First Class Anonymous Functions
First Class Anonymous Functions
1 sqr a = a * a 2 hypotsq a b = sqr a + sqr b
1 sqr :: Integer -> Integer 2 sqr :: Num a => a -> a 3 hypotsq :: Num a => a -> a -> a 4 Prelude> sqr 10 5 100 6 Prelude> hypotsq 3 4 7 25
First Class Anonymous Functions
1 inc x = x + 1 2 double x = x * 2 3 compose f g x = f (g x)
1 compose :: (t1 -> t2) -> (t -> t1) -> t -> t2 2 Prelude> :t double 3 double :: Integer -> Integer 4 Prelude> double 10 5 20 6 Prelude> compose inc double 10 7 21
First Class Anonymous Functions
1 twice f x = f (f x)
First Class Anonymous Functions
1 \x -> x + 1
◮ Backslash (a.k.a. lambda) ◮ Parameter list ◮ Arrow ◮ Body of function 1 prelude> (\x -> x + 1) 41 2 42
First Class Anonymous Functions
1 inc :: (Num t) => t -> t 2 inc a = a + 1 3 inc = \a -> a + 1 4 5 plus :: (Num t) => t -> t -> t 6 plus a b = a + b 7 plus = \a -> \b -> a + b
First Class Anonymous Functions
1 inc :: (Num t) => t -> t 2 inc a = a + 1 3 inc = \a -> a + 1 4 5 plus :: (Num t) => t -> t -> t 6 plus a b = a + b 7 plus = \a -> \b -> a + b
1 inc = plus 1
First Class Anonymous Functions
1 plus a b = (+) a b 2 plus a = (+) a 3 plus = (+)
1 inc x = x + 1 2 inc = (+) 1 3 inc = (+1)
First Class Anonymous Functions
1 tplus :: (Integer,Integer) -> Integer 2 tplus (a,b) = a + b