chapter7

# chapter7 - PROGRAMMING IN HASKELL 1 Introduction...

This preview shows pages 1–8. Sign up to view the full content.

1 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Introduction A function is called higher-order  if it takes a function  as an argument or returns a function as a result. twice    :: (a   a)   a   a twice f x = f (f x) twice is higher-order because it takes a function as its first argument.
3 Why Are They Useful? Common programming idioms  can be encoded  as functions within the language itself. Domain specific languages  can be defined as  collections of higher-order functions. Algebraic properties  of higher-order functions  can be used to reason about programs.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 The Map Function The higher-order library function called map  applies  a function to every element of a list. map :: (a   b)   [a]   [b] For example: > map (+1) [1,3,5,7] [2,4,6,8]
5 Alternatively, for the purposes of proofs, the map  function can also be defined using recursion:  The map function can be defined in a particularly  simple manner using a list comprehension: map f xs = [f x | x   xs] map f []     = [] map f (x:xs) = f x : map f xs

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 The Filter Function The higher-order library function filter  selects every  element from a list that satisfies a predicate. filter :: (a   Bool)   [a]   [a] For example: > filter even [1. .10] [2,4,6,8,10]
Alternatively, it can be defined using recursion: Filter can be defined using a list comprehension: filter p xs = [x | x   xs, p x]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

chapter7 - PROGRAMMING IN HASKELL 1 Introduction...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online