# Example: How recursive functions work.

# A non-recursive function:
def sumsq(lst):
    s = 0
    for x in lst:
        s += x**2
    return s

from traced import traced

# A recursive function that is equivalent to sumsq.
#@traced            #(C)
def sumsqR(lst):
    #print("sumsqR({})".format(lst))        #(A)
    s = 0
    if len(lst) > 0:
        sq0 = lst[0]**2
        s = sq0 + sumsqR(lst[1:])
    #print("sumsqR({})->{}".format(lst, s)) #(B)
    return s

# Testing both functions:
print( sumsq([1, 2, 3]) )
print( sumsqR([1, 2, 3]) )

# To see how sumsqR works, uncomment the print statements (A) and (B).
# This will show: (A) each time the function is CALLED, and
# (B) each time the function RETURNS.

# Another way to do this: remove (or comment) the print statements,
# and uncomment line (C).
# (The @traced annotation will do the prints automatically.)
# (That annotation is implemented in module traced.py.)

