Help- Simple recursive function to build a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • actuary77

    #1

    Help- Simple recursive function to build a list

    I am trying to write simple recursive function to build a list:


    def rec(n,alist=[]):
    _nl=alist[:]
    print n,_nl
    if n == 0:
    print n,_nl
    return _nl
    else:
    _nl=_nl+[n]
    rec(n-1,_nl)

    _nl=[]
    _nl=rec(4)
    print _nl

    ### shouldn't this work?

    _nl=rec(4)




    The output is:
    4 []
    3 [4]
    2 [4, 3]
    1 [4, 3, 2]
    0 [4, 3, 2, 1]
    0 [4, 3, 2, 1]
    None
    None


    Question:
    ============
    Why isn't the function returning a list?
    Why is function returning None?

    Any guidance from one of you pro's would be greatly appreciated.

    Thanks,
    DKoch
    actuary77@eleme ntal-systems.com
  • Roy Smith

    #2
    Re: Help- Simple recursive function to build a list

    In article <mailman.3222.1 109731598.22381 .python-list@python.org >,
    actuary77 <actuary77@elem ental-systems.com> wrote:
    [color=blue]
    > I am trying to write simple recursive function to build a list:
    >
    >
    > def rec(n,alist=[]):
    > _nl=alist[:]
    > print n,_nl
    > if n == 0:
    > print n,_nl
    > return _nl
    > else:
    > _nl=_nl+[n]
    > rec(n-1,_nl)
    >
    > _nl=[]
    > _nl=rec(4)
    > print _nl
    >
    > ### shouldn't this work?
    >
    > _nl=rec(4)
    >
    >
    >
    >
    > The output is:
    > 4 []
    > 3 [4]
    > 2 [4, 3]
    > 1 [4, 3, 2]
    > 0 [4, 3, 2, 1]
    > 0 [4, 3, 2, 1]
    > None
    > None
    >
    >
    > Question:
    > ============
    > Why isn't the function returning a list?
    > Why is function returning None?[/color]

    I'm not 100% sure what you're trying to do here, but I suspect you left a
    "return" out of the else block, i.e. "return rec(n-1, _nl)". That would
    certainly explain why it's returning None.

    What problem are you trying to solve, or are you just experimenting with
    recursion?

    Comment

    • Kent Johnson

      #3
      Re: Help- Simple recursive function to build a list

      actuary77 wrote:[color=blue]
      > I am trying to write simple recursive function to build a list:
      >
      >
      > def rec(n,alist=[]):
      > _nl=alist[:]
      > print n,_nl
      > if n == 0:
      > print n,_nl
      > return _nl
      > else:
      > _nl=_nl+[n]
      > rec(n-1,_nl)[/color]

      should be
      return rec(n-1,_nl)
      [color=blue][color=green][color=darkred]
      >>> def rec(n,alist=[]):[/color][/color][/color]
      ... _nl=alist[:]
      ... print n,_nl
      ... if n == 0:
      ... print n,_nl
      ... return _nl
      ... else:
      ... _nl=_nl+[n]
      ... return rec(n-1,_nl)
      ...[color=blue][color=green][color=darkred]
      >>> _nl = rec(4)[/color][/color][/color]
      4 []
      3 [4]
      2 [4, 3]
      1 [4, 3, 2]
      0 [4, 3, 2, 1]
      0 [4, 3, 2, 1][color=blue][color=green][color=darkred]
      >>> print _nl[/color][/color][/color]
      [4, 3, 2, 1]

      Kent

      Comment

      Working...