Last active
December 25, 2015 19:48
-
-
Save waveform80/7029835 to your computer and use it in GitHub Desktop.
Factorial in a functional style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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