chapter7 - PROGRAMMING IN HASKELL...

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

View Full Document Right Arrow Icon
1 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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]
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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]
Background image of page 6
Alternatively, it can be defined using recursion: Filter can be defined using a list comprehension: filter p xs = [x | x   xs, p x]
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

chapter7 - PROGRAMMING IN HASKELL...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online