Presentation is loading. Please wait.

Presentation is loading. Please wait.

RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments.

Similar presentations


Presentation on theme: "RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments."— Presentation transcript:

1 RECURSIVE FUNCTIONS

2 A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments where we do not have to call the function again. 2. An inductive step, where for arguments not handled by the basis, we call the function recursively with smaller arguments. By smaller arguments we mean a smaller number or a smaller list such as the tail of a list. We start with simple recursion. - fun reverse(L) = = if L = nil then nil = else reverse(tl(L)) @ [hd(L)]; val reverse = fn : ''a list -> ''a list - reverse([1,2,3]); val it = [3,2,1] : int list - reverse(["dog","cat","bird"]); val it = ["bird","cat","dog"] : string list

3 Function Execution Lnil L[3] L[2,3] L[1,2,3] reverse definition of reverse Current View Environment Before call

4 Nonlinear Recursion In the preceding example the program was called only once in each recursive step. We will choose for our example the combinations of m things taken one at a time which can be calculated by the following formula. n C m = n-1 C m + n-1 C m-1 - fun comb(n,m) = (* assumes 0 <= m <= n *) = if m = 0 orelse m = n then 1 = else comb(n-1,m) + comb(n-1,m-1); val comb = fn : int * int -> int - comb(4,2); val it = 6 : int

5 Structure of Recursive calls for comb(4,2) comb(4) comb(3,2) comb(3,1) comb(2,2) comb(2,1) comb(2,1) comb(2,0) comb(1,1) comb(1,0) comb(1,1) comb(1,0)

6 Mutual Recursion Suppose we wish to write two functions that call one another, which we call mutual recursion. Most languages do not provide such a mechanism, but ML does. Suppose we wish to write a function that takes a list L as an argument and produces a list of alternate elements of L. There are two(at least) ways to do this. One, which we call take(L), takes the first, third, fifth, etc. The other, which we call skip(L), skips the first and then takes alternate elements of L. - fun take(L) = = if L = nil then nil = else hd(L) ::skip(tl(L)); stdIn:15.15-15.19 Error: unbound variable or constructor: skip

7 Correct definition of a mutually recursive function: - fun = take(L) = = if L = nil then nil = else hd(L) :: skip(tl(L)) = GC #0.0.0.0.1.13: (0 ms) and = skip(L) = = if L = nil then nil = else take(tl(L)); val take = fn : ''a list -> ''a list val skip = fn : ''a list -> ''a list - take([1,2,3,4,5,6]); GC #0.0.0.0.2.27: (0 ms) val it = [1,3,5] : int list - skip(["a","b","c","d"]); val it = ["b","d"] : string list


Download ppt "RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments."

Similar presentations


Ads by Google