{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chapter6

# chapter6 - PROGRAMMING IN HASKELL 1 Introduction...

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

1 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions

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

View Full Document
2 Introduction As we have seen, many functions can naturally be  defined in terms of other functions. factorial  :: Int   Int factorial n = product [1. .n] factorial maps any integer n to the  product of the integers between 1 and n.
3 Expressions are evaluated  by a stepwise process of  applying functions to their arguments. For example: factorial 4 product [1. .4] = product [1,2,3,4] = 1*2*3*4 = 24 =

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

View Full Document
4 Recursive Functions In Haskell, functions can also be defined in terms of  themselves.  Such functions are called recursive . factorial 0     = 1 factorial (n+1) = (n+1) * factorial n factorial maps 0 to 1, and any other  positive integer to the product of itself  and the factorial of its predecessor.
5 For example: factorial 3 3 * factorial 2 = 3 * (2 * factorial 1) = 3 * (2 * (1 * factorial 0)) = 3 * (2 * (1 * 1)) = 3 * (2 * 1) = = 6 3 * 2 =

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

View Full Document
6 Note: factorial 0 = 1 is appropriate because 1 is the  identity for multiplication: 1 * x = x = x * 1. The recursive definition diverges  on integers  <  0  because the base case is never reached: > factorial (-1) Error: Control stack overflow
7 Why is Recursion Useful? Some functions, such as factorial, are simpler

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 / 22

chapter6 - 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