80 likes | 190 Views
RECURSIVE FUNCTIONS. 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.
E N D
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
Function Execution Current View Environment Before call
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. nCm = n-1Cm + n-1Cm-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
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)
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
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