SlideServe
Menu
Préférences
© SlideServe · v2026

RECURSIVE FUNCTIONS

zuri avatar
7 diapositives · 216 Views · Sep 10, 2014

Learn about recursive functions in programming, from simple recursion to mutual recursion in functional programming with examples in ML.

RECURSIVE FUNCTIONS - slide 1
01 / 7RECURSIVE FUNCTIONS
1 / 17

Télécharger la présentation

RECURSIVE FUNCTIONS
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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 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. 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

  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