Skip to content

Instantly share code, notes, and snippets.

@waveform80
Last active December 25, 2015 19:48
Show Gist options
  • Select an option

  • Save waveform80/7029835 to your computer and use it in GitHub Desktop.

Select an option

Save waveform80/7029835 to your computer and use it in GitHub Desktop.
Factorial in a functional style
def factorial(i):
if i > 1:
return factorial(i - 1) * i
elif i == 1:
return 1
else:
raise ValueError('Cannot take factorial of %d' % i)
def filter_odd(l):
if l == []:
return []
elif l[0] % 2:
return [l[0]] + filter_odd(l[1:])
else:
return [] + filter_odd(l[1:])
# The way it should be...
#return [i for i in l if i % 2]
def sum(l):
if len(l) >= 1:
return l[0] + sum(l[1:])
else:
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment