Below is the function count_leaf, that appends mutable list branch_counts, which is not stateless.
def count_leaf(tree):
if is_leaf(tree):
return 1
branch_counts = list()
for b in tree:
branch_counts.append(count_leaf(b))
return sum(branch_counts)
Below is the program that uses list comprehension.
def count_leaves(tree):
if is_leaf(tree):
return 1
else:
branch_counts = [count_leaves(b) for b in tree]
return sum(branch_counts)
Is the function count_leaves using list comprehension [count_leaves(b) for b in tree] purely functional?