Skip to content

Instantly share code, notes, and snippets.

@noahadams
Created March 7, 2018 18:25
Show Gist options
  • Save noahadams/584241efa9003be7c1e376a4dc587ec0 to your computer and use it in GitHub Desktop.
Save noahadams/584241efa9003be7c1e376a4dc587ec0 to your computer and use it in GitHub Desktop.
6 Small Python 3 programs that output the numbers 1 to 10
# Example 1
print('1\n2\n3\n4\n5\n6\n7\n8\n9\n10')
# Example 2
i = 1
while i <= 10:
print(i)
i = i + 1
# Example 3
for i in range(1, 11):
print(i)
# Example 4
def numbers(n): return '\n'.join(str(i) for i in range(1, n + 1))
print(numbers(10))
# Example 5
import collections
collections.deque(map(print, range(1, 11)), maxlen=0)
# Example 6
succ = lambda n: lambda f: lambda x: f(n(f)(x))
zero = lambda f: lambda x: x
one = succ(zero)
two = succ(one)
three = succ(two)
four = succ(three)
five = succ(four)
six = succ(five)
seven = succ(six)
eight = succ(seven)
nine = succ(eight)
ten = succ(nine)
cons = lambda l, r: lambda m: m(l, r)
car = lambda c: c(lambda l, _: l)
cdr = lambda c: c(lambda _, r: r)
foldl = lambda l, i, f: i if l is zero else foldl(cdr(l), f(i, car(l)), f)
foldr = lambda l, i, f: i if l is zero else f(foldr(cdr(l), i, f), car(l))
mapr = lambda l, f: foldr(l, zero, lambda a, b: cons(f(b), a))
foreach = lambda l, f: foldl(l, zero, lambda a, b: f(b))
numbers = (cons(one, cons(two, cons(three, cons(four, cons(five, cons(six,
cons(seven, cons(eight, cons(nine, cons(ten, zero)))))))))))
strings = {one: '1', two: '2', three: '3', four: '4', five: '5', six: '6',
seven: '7', eight: '8', nine: '9', ten: '10'}
church_num_2_str = lambda x: strings.get(x, '')
foreach(mapr(numbers, church_num_2_str), print)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment