03-recursion

Embed Size (px)

Citation preview

  • 7/29/2019 03-recursion

    1/12

    1

    Recursion

  • 7/29/2019 03-recursion

    2/12

    2

    Definitions I

    A recursive definition is a definition in which the

    thing being defined occurs as part of its own

    definition

    Example: A list consists of: An open parenthesis, "("

    Zero or more atoms or lists, and

    A close parenthesis, ")"

  • 7/29/2019 03-recursion

    3/12

    3

    Definitions II

    Indirect recursion is when a thing is defined in

    terms of other things, but those other things are

    defined in terms of the first thing

    Example: A list is: An open parenthesis,

    Zero or more S-expressions, and

    A close parenthesis

    An S-expression is an atom or a list

  • 7/29/2019 03-recursion

    4/12

    4

    Understanding recursion

    The usual way to teach recursion is to trace

    through a recursion, seeing what it does at each

    level This may be a good way to understand how

    recursion works...

    ...but it's a terrible way to try to use recursion There is a better way

  • 7/29/2019 03-recursion

    5/12

    5

    Base cases and recursive cases

    Every valid recursive definition consists of two

    parts:

    One or more base cases, where you compute the

    answer directly, without recursion

    One or more recursive cases, where you dopartof the

    work, and recur with a simpler problem

  • 7/29/2019 03-recursion

    6/12

    6

    The four rules

    Do the base cases first

    Recur only with a simpler case

    Don't use global or reference variables

    Don't look down

  • 7/29/2019 03-recursion

    7/12

    7

    Do the base cases first

    Every recursive function musthave some things itcan do without recursion

    These are the simple, orbase, cases

    Test for these cases, and do them first This is just writing ordinary, nonrecursive code

  • 7/29/2019 03-recursion

    8/12

    8

    Recur only with a simpler case

    If the problem isn't simple enough to be a basecase, break it into two parts:

    Asimplerproblem of the same kind (for example, a

    smaller number, or a shorter list)

    Extra worknot solved by the simpler problem

    Combine the results of the recursion and the extra

    work into a complete solution

    Simpler means more like a base case

  • 7/29/2019 03-recursion

    9/12

    9

    It's OK to use locals variables and

    parameters passed by value

    A function has its own copy of

    local variables

    parameters passed by value Each level of a recursive function has its own copy

    of these variables and parameters

    Changing them at one level does not change them

    at other levels

    One level can'tinterfere with another level

  • 7/29/2019 03-recursion

    10/12

    10

    It's bad to use global variables or

    parameters passed by reference

    There is only one copy of a global variable

    If a parameter is passed by reference, there is only

    one copy of it

    If such a variable is changed by a recursive function,

    it's changed at all levels

    The various levels interfere with one another This can get very confusing

    Don't let this happen to you!

  • 7/29/2019 03-recursion

    11/12

    11

    Don't look down

    When you write or debug a recursive function,think about this level only

    Wherever there is a recursive call, assume that it

    works correctly If you can get this level correct, you will

    automatically get alllevels correct

    You really can't understand more than one level at

    a time, so don't even try

  • 7/29/2019 03-recursion

    12/12

    12

    The End