CSc 372 - Comparative Programming Languages
11 : Haskell -- Higher-Order Functions

Christian Collberg

Department of Computer Science

University of Arizona

1 Higher-Order Functions


\begin{gprogram}
deriv :: (Float->Float)->Float->Float \\
deriv f x = (f(x+dx) - f x)/0.0001
\end{gprogram}

2 Currying Revisited

Uh, what was this currying thing?

3 Currying Revisited...

How is a curried function defined?


\begin{gprogram}
fun :: $\tc{t}_1$\ -> $\tc{t}_2$\ -> $\cdots$\ -> $\tc{t}_n$\ -> t
\end{gprogram}

\begin{gprogram}
$\tc{fun}_1$\ :: $\tc{t}_2$\ -> $\cdots$\ -> $\tc{t}_n$\ -> t \...
...n$\ -> t \\
$\tc{fun}_2$\ $\tc{a}_3\cdots \tc{a}_n$\ = $\cdots$
\end{gprogram}

4 Currying Revisited...

Duh, how about an example?


\begin{gprogram}
get\_nth 1 (x:\_) = x\\
get\_nth n (\_:xs) = get\_nth (n-1) xs \\
\\
get\_nth 10 ''Bartholomew'' $\Rightarrow$\ 'e'
\end{gprogram}


 
\begin{bprogram}
get\_second = \= get\_nth 2 \\
get\_third = \> get\_nth 3
\end{bprogram}

\begin{bprogram}
get\_fourth = \= get\_nth 4 \\
get\_fifth = \> get\_nth 5
\end{bprogram}

5 Currying Revisited...


\begin{gprogram}
get\_fifth ''Bartholomew'' $\Rightarrow$\ 'h' \\
\\
map (get\...
... [''mob'',''sea'',''tar'',''bat''] $\Rightarrow$\ \\
\x ''bart''
\end{gprogram}

So, what's the type of get_second?

6 Patterns of Computation

Mappings

Selections

Folds

7 The map Function


\begin{gprogram}
map :: (a -> b) -> [a] -> [b] \\
map f [\ ] \xxxxxx = [\ ] \\
map f (x:xs) \xxxxxx = f x : map f xs
\end{gprogram}

8 The map Function...

 
\begin{bprogram}
map :: (a -> b) -> [a] -> [b] \\
map f [\ ] \xxxxxx = [\ ] \\ ...
...
inc x = x + 1 \\
\\
map inc [1,2,3,4] $\Rightarrow$\ [2,3,4,5]
\end{bprogram}
Map

9 The map Function...


\begin{gprogram}
map :: (a -> b) -> [a] -> [b] \\
map f [\ ] \xxxxxx = [\ ] \\
map f (x:xs) \xxxxxx = f x : map f xs
\end{gprogram}

map f [ ] = [ ]
means: ``The result of applying the function f to the elements of an empty list is the empty list.''
map f (x:xs) = f x : map f xs
means: ``applying f to the list (x:xs) is the same as applying f to x (the first element of the list), then applying f to the list xs, and then combining the results.''

10 The map Function...

Simulation:
\begin{gprogram}
map square [5,6] $\Rightarrow$\ \\
\x square 5 : map square [6...
...) $\Rightarrow$\ \\
\xx 25 : [36] $\Rightarrow$\ \\
\x [25,36]
\end{gprogram}

11 The filter Function

Examples:
\begin{gprogram}
\redtxt{filter even [1..10]} $\Rightarrow$\ [2,4,6,8,10] \\
\r...
...\\
\x \redtxt{where gt10 x = x > 10} $\Rightarrow$\ [11,23,114]
\end{gprogram}

12 The filter Function...

Using recursion:
\begin{gprogram}
filter :: (a -> Bool) -> [a] -> [a] \\
filter \_ [] = [] \\
f...
...xxxx = x : filter p xs \\
\x \vert otherwise \xxxx = filter p xs
\end{gprogram}

Using list comprehension:
\begin{gprogram}
filter :: (a -> Bool) -> [a] -> [a] \\
filter p xs = [x \vert x <- xs, p x]
\end{gprogram}

13 The filter Function...

 
\begin{bprogram}
filter :: (a->Bool)->[a]->[a] \\
filter \_ [] = [] \\
filter ...
... p xs \\
\\
\redtxt{filter even [1,2,3,4] $\Rightarrow$\ [2,4]}
\end{bprogram}
Filter

14 The filter Function...


\begin{gprogram}
getEven :: [Int] -> [Int] \\
getEven xs = filter even xs \\
\...
...r pos xs) \\
\x where \= dbl x = 2 * x \\
\x \x pos x = x > 0
\end{gprogram}

Simulations:
\begin{gprogram}
\redtxt{getEven [1,2,3]} $\Rightarrow$\ [2] \\
\\
\redtxt{dou...
...,2,3,4]) $\Rightarrow$\ \\
\x map dbl [2,4] $\Rightarrow$\ [4,8]
\end{gprogram}

15 fold Functions

Examples:
\begin{gprogram}
sum [1,2,3,4,5] $\equiv$\ \\
\xx (1 + (2 + (3 + (4 + (5 + 0)))...
...
\xx (''H'' ++ (''i'' ++ (''!'' ++ ''''))) $\Rightarrow$\ ''Hi!''
\end{gprogram}

16 fold Functions...

Examples:
\begin{gprogram}
\redtxt{foldr (+) 0 [1,2,3,4,5]} $\Rightarrow$\ 15 \\
\redtxt{foldr (++) '''' [''H'',''i'',''!'']} $\Rightarrow$\ ''Hi!''
\end{gprogram}

foldr:
\begin{gprogram}
foldr :: (a->b->b) -> b -> [a] -> b \\
foldr f z [\ ] \xxxxxx = z \\
foldr f z (x:xs) \xxxxxx = f x (foldr f z xs)
\end{gprogram}

17 fold Functions...


\begin{displaymath}
\tc{foldr} (\oplus) \tc{z} [\tc{x}_1\cdots\tc{x}_n] =
(\tc{x}_1 \oplus (\tc{x}_2 \oplus (\cdots (\tc{x}_n\oplus \tc{z}))))
\end{displaymath}


\begin{gprogram}
and,or :: [Bool] -> Bool \\
and xs = foldr (\&\&) True xs \\
or xs = foldr (\vert\vert) False xs
\end{gprogram}

\begin{gprogram}
\redtxt{? or [True,False,False]} $\Rightarrow$\ \\
\x foldr (\...
...t (False \vert\vert (False \vert\vert False)) $\Rightarrow$\ True
\end{gprogram}

18 fold Functions...


\begin{gprogram}
foldr (+) 0 [1,2,3] $\Rightarrow$\ (1+(2+(3+0)))
\end{gprogram}

\begin{gprogram}
foldl (+) 0 [1,2,3] $\Rightarrow$\ (((0+1)+2)+3)
\end{gprogram}


\begin{displaymath}
\tc{foldl} (\oplus) \tc{z} [\tc{x}_1\cdots\tc{x}_n] =
(((...
...lus \tc{x}_1) \oplus \tc{x}_2) \oplus \cdots \oplus
\tc{x}_n)
\end{displaymath}

19 fold Functions...

\begin{eqnarray*}
\tc{foldl} (\oplus) \tc{z} [\tc{x}_1\cdots\tc{x}_n] &=& \tc{foldr} (\oplus) \tc{z} [\tc{x}_1\cdots\tc{x}_n]
\end{eqnarray*}

20 fold Functions...

fold

21 Operator Sections

(*2) -
function that doubles its argument
(>2) -
function that returns True for numbers $>2$.

(op a) b = b op a
\begin{gprogram}
(*2) 4 \xxxxxx = 4 * 2 \xxx = 8 \\
(>2) 4 \xxxxxx = 4 > 2 \xxx...
...\backslash$n'') ''Bart'' \xxxxxx = ''Bart'' ++ ''$\backslash$n''
\end{gprogram}

22 Operator Sections...

(a op) b = a op b
\begin{gprogram}
(3:) [1,2] \xxxx = 3 : [1,2] \xxxx = [3,1,2] \\
(0<) 5 \xxxx = 0 < 5 \xxxx = True \\
(1/) \xxxx = 1/5
\end{gprogram}

Examples:

(+1) -
The successor function.
(/2) -
The halving function.
(:[]) -
The function that turns an element into a singleton list.

More Examples:
\begin{gprogram}
\redtxt{? filter (0<) (map (+1) [-2,-1,0,1])} \\
\x [1,2]
\end{gprogram}

23 takeWhile & dropWhile


\begin{gprogram}
take 2 ['a','b','c'] $\Rightarrow$\ ['a','b'] \\
drop 2 ['a','b','c'] $\Rightarrow$\ ['c']
\end{gprogram}


\begin{gprogram}
takeWhile even [2,4,6,5,7,4,1] $\Rightarrow$\ \\
\x [2,4,6] \\
dropWhile even [2,4,6,5,7,4,1] $\Rightarrow$\ \\
\x [5,7,4,1]
\end{gprogram}

24 takeWhile & dropWhile...


\begin{gprogram}
takeWhile :: (a->Bool) -> [a] -> [a] \\
takeWhile p [\ ] = [\ ...
...rt p x \xxxx = dropWhile p xs \\
\x \vert otherwise \xxxx = x:xs
\end{gprogram}

25 takeWhile & dropWhile...


\begin{gprogram}
dropWhile ((==) \verb*\vert' '\vert) \verb*\vert'' Hi!''\vert $...
...t' '\vert) \verb*\vert''Hi! ''\vert $\Rightarrow$\ \\
\x ''Hi!''
\end{gprogram}

26 Summary

27 Summary...

28 Homework

Homework (a):

Template:
\begin{gprogram}
map f x = [ $\cdots$\ \vert $\cdots$\ ]
\end{gprogram}

Homework (b):

Examples:
\begin{gprogram}
\redtxt{? lengthall [''Ay'', ''Caramba!'']} \\
\x [2,8]
\end{gprogram}

29 Homework

  1. Give a accumulative recursive definition of foldl.
  2. Define the minimum xs function using foldr.
  3. Define a function sumsq n that returns the sum of the squares of the numbers $[1\cdots n]$. Use map and foldr.
  4. What does the function mystery below do?

\begin{gprogram}
mystery xs = \\
\x foldr (++) [] (map sing xs)\\
sing x = [x]
\end{gprogram}

Examples:
\begin{gprogram}
minimum [3,4,1,5,6,3] $\Rightarrow$\ 1
\end{gprogram}

30 Homework...

Examples:
\begin{gprogram}
zipp (+) [1,2,3] [4,5,6] $\Rightarrow$\ [5,7,9] \\
\\
zipp (=...
...ue] \\
\\
zipp (==) [1,2,3] [4,2] $\Rightarrow$\ \redtxt{ERROR}
\end{gprogram}

31 Homework

Example:
\begin{gprogram}
filterFirst even [2,4,6,5,6,8,7] $\Rightarrow$\ \\
\x [2,4,6,6,8,7]
\end{gprogram}

Example:
\begin{gprogram}
filterLast even [2,4,6,5,6,8,7] $\Rightarrow$\ \\
\x [2,4,6,5,6,8]
\end{gprogram}



Christian S. Collberg
2005-09-16