



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The purpose of this lecture is to give you an introduction to higher order functions. As a result of this lecture, you should...
A type is said to be first class type when it can be
Examples include:
The Kind of Data a Program Manipulates Changes the Expressive Ability of a Program
1 compose f g x = f (g x) 2 inc = (+ 1) 3 double = (∗ 2)
*Main> double 40 80 *Main> inc 80 81 *Main> compose inc double 40 81 Understanding the type will be important.
*Main> :t compose compose :: (t1 -> t2) -> (t -> t1) -> t -> t
Activity 1 Try writing a function called twice. It should take two arguments, a function and a victim. It will apply the function twice to the victim. What will be its type?
*Main> twice inc 10 12
Consider these two definitions:
1 cplus a b = a + b 2 uplus (a,b) = a + b
*Main> :t cplus cplus :: (Num a) => a -> a -> a *Main> :t uplus uplus :: (Num t) => (t, t) -> t
Why might you want one version rather than the other?
Activity 2 Function cplus is said to be in curried form. Function uplus is said to be in uncurried form. Write a function curry that takes an uncurried function and returns a curried version of it. Do the same thing for uncurry. What are the types?
*Main> curry uplus 10 20 30
Consider the code to sum the elements of a list, and the code to take the product of the elements of a list. Again, what is different about them?
Activity 4 Write a function foldr that takes an operation f and a list [x 0 , x 1 ,... , xn] and a base case z and returns the result f x 0 (f x 1 (· · · (f xnz) · · ·)). Be sure to indicate the type. Show how to implement sumlist and prodlist using foldr.
Consider the builtin function zip.
*Main> :t zip zip :: [a] -> [b] -> [(a, b)] *Main> zip [1,2,3] [2,4,6] [(1,2),(2,4),(3,6)]
Write the code for zip, and then try writing a function zipwith that takes a function and applies it to the tuples. Use only the higher order functions we’ve looked at so far.
*Main> zipwith (+) [1,2,3] [2,4,6] [3,6,9]
These are related to higher order functions.
1 inclist xx = [x + 1 | x ← xx]
The x <- xx part is called a generator. You can also specify filters.
1 incodd xx = [x + 1 | x ← xx, x ‘mod‘ 2 == 1]
*Main> incodd [2,3,4,5,6] [4,6]
Combined with lazy evaluation, you can easily make infinite lists that behave like iterators.
1 ones = 1 : ones 2 nats = 1 : [x + 1 | x ← nats] 3 nats’ = 1 : map (+ 1) nats’
Activity 5 Consider Newton’s method xn = xn− 1 − f (xn− 1 )/f ′(xn− 1 ). Make an infinite list that returns successive approximations of the zero of f (x) = x^3 + 2x^2 − 5 x, starting from 10.
1 f x = x ∗∗ 3 + 2 ∗ x ∗∗ 2 - 5 ∗ x 2 f’ x = 3 ∗ x ∗∗ 2 + 4 ∗ x - 5 3 zeros = undefined
take 10 zeros [10.0,6.567164179104477,4.332565849912022,2.916461430054191, 2.0701824265863307,1.630684317828152,1.4726817772381844, 1.449954736062582,1.4494899359552085,1.4494897427832114]