Docsity

Study Guide for Exam - Programming Languages and Translators | CS 440, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Higher Order Functions January 28, 2008
1 Introduction and Objectives
The purpose of this lecture is to give you an introduction to higher order functions. As a result of
this lecture, you should. . .
•know how to create higher order functions
•know how to use and write the following kinds of higher order functions:
–function combination — twice,compose
–interface changes — curry,uncurry
–list processing — foldr,map,zip
2 First Class Values
A type is said to be first class type when it can be
•assigned to a variable, passed as a parameter, or returned as a result
Examples include:
•APL: scalars, vectors, arrays
•C: scalars, pointers, structures
•C++: like C, but with classes
•Haskell, Scheme, Lisp, ML: scalars, lists, tuples, functions
The Kind of Data a Program Manipulates Changes the Expressive Ability of a
Program
1compose f g x =f (g x)
2inc =(+1)
3double =(∗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 -> t2
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5

Partial preview of the text

Download Study Guide for Exam - Programming Languages and Translators | CS 440 and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

The purpose of this lecture is to give you an introduction to higher order functions. As a result of this lecture, you should...

  • know how to create higher order functions
  • know how to use and write the following kinds of higher order functions:
    • function combination — twice, compose
    • interface changes — curry, uncurry
    • list processing — foldr, map, zip

2 First Class Values

A type is said to be first class type when it can be

  • assigned to a variable, passed as a parameter, or returned as a result

Examples include:

  • APL: scalars, vectors, arrays
  • C: scalars, pointers, structures
  • C++: like C, but with classes
  • Haskell, Scheme, Lisp, ML: scalars, lists, tuples, functions

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

3 Curry and Uncurry

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

4.2 Folding

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.

4.3 Zipping

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]

5 List Comprehensions

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]