Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added more interesting continuation example

  • Loading branch information...
commit 40102c1518c0501dc882eae30ac4b966bb5ca689 1 parent 10c6ecb
@dpiponi authored
Showing with 32 additions and 28 deletions.
  1. +2 −0  mpython.py
  2. +3 −28 test1.py
  3. +27 −0 test2.py
View
2  mpython.py
@@ -76,4 +76,6 @@ def fail():
e = compile(source, "<string>", "exec", PyCF_ONLY_AST)
e = RewriteComp().visit(e)
f = compile(e, "<string>", "exec")
+print f
exec f
+print "Done"
View
31 test1.py
@@ -1,29 +1,4 @@
-a = [x for y in [1,2,3]
- if y>2
- for x in [y,10*y]
- if x>15]
+a = [(x, y) for x in [0,1]
+ for y in [0,1]]
-print a
-
-def concatMap(k, m):
- return lambda c:m(lambda a:k(a)(c))
-
-def singleton(x):
- return lambda f:f(x)
-
-def callCC(f):
- return lambda c:f(lambda a:lambda _:c(a))(c)
-
-def fail():
- raise "Oh shit!"
-
-a = callCC(lambda k: [(u,y) for x in singleton(11)
- for u in (k(1000) if x>10 else singleton(x))
- for y in singleton(2)])
-
-print a(str)
-
-a = callCC(lambda k: [x for x in singleton(11)
- if x > 10])
-
-print a(str)
+print a
View
27 test2.py
@@ -0,0 +1,27 @@
+import math
+
+def concatMap(k, m):
+ return lambda c:m(lambda a:k(a)(c))
+
+def singleton(x):
+ return lambda f:f(x)
+
+def callCC(f):
+ return lambda c:f(lambda a:lambda _:c(a))(c)
+
+def fail():
+ raise "Failure is not an option for continuations"
+
+def id(x):
+ return x
+
+def solve(a, b, c):
+ return callCC(lambda throw: [((-b-d)/(2*a), (-b+d)/(2*a))
+ for a0 in (singleton(a) if a!=0 else throw("Not quadratic"))
+ for d2 in singleton(b*b-4*a*c)
+ for d in (singleton(math.sqrt(d2)) if d2>=0 else throw("No roots"))
+ ])
+
+print solve(1, 0, -9)(id)
+print solve(1, 1, 9)(id)
+print solve(0, 1, 9)(id)
Please sign in to comment.
Something went wrong with that request. Please try again.