Last active
August 29, 2015 14:16
-
-
Save kroger/1ad4d711bd07a06bf222 to your computer and use it in GitHub Desktop.
This file contains 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 p(): | |
return p() | |
def test(x, y): | |
return 0 if x == 0 else y | |
test(0, p()) |
This file contains 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
from operator import add, sub | |
def a_plus_abs_b(a, b): | |
return (add if b > 0 else sub)(a, b) |
This file contains 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
(define (a-plus-abs-b a b) | |
((if (> b 0) + -) a b)) |
This file contains 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
# applicative order normal order | |
f(5) f(5) | |
sum_of_squares(5+1, 5*2) sum_of_squares(5+1, 5*2) | |
sum_of_squares(6, 10) square(5+1) + square(5*2) | |
square(6) + square(10) ((5+1) * (5+1)) + ((5*2) * (5*2)) | |
(6 * 6) + (10 * 10) (6 * 6) + (10 * 10) | |
36 + 100 36 + 100 | |
136 136 |
This file contains 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 average(x, y): | |
return (x + y)/2 |
This file contains 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 sqrt(x): | |
def is_good_enough(guess, x): | |
return abs(square(guess) - x) < 0.001 | |
def improve(guess, x): | |
return average(guess, x/guess) | |
def sqrt_iter(guess, x): | |
if is_good_enough(guess, x): | |
return guess | |
else: | |
return sqrt_iter(improve(guess, x), x) | |
return sqrt_iter(1.0, x) |
This file contains 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
>>> import dis | |
>>> dis.dis(square) | |
1 0 LOAD_FAST 0 (x) | |
3 LOAD_FAST 0 (x) | |
6 BINARY_MULTIPLY | |
7 RETURN_VALUE | |
>>> dis.dis(square2) | |
1 0 LOAD_FAST 0 (x) | |
3 LOAD_FAST 0 (x) | |
6 BINARY_MULTIPLY | |
7 RETURN_VALUE |
This file contains 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 is_good_enough(guess, x): | |
return abs(square(guess) - x) < 0.001 |
This file contains 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 improve(guess, x): | |
return average(guess, x/guess) |
This file contains 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
square2 = lambda x: x * x |
This file contains 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
lambda x: x * x |
This file contains 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 sqrt(x): | |
def is_good_enough(guess): | |
return abs(square(guess) - x) < 0.001 | |
def improve(guess): | |
return average(guess, x/guess) | |
def sqrt_iter(guess): | |
if is_good_enough(guess): | |
return guess | |
else: | |
return sqrt_iter(improve(guess)) | |
return sqrt_iter(1.0) |
This file contains 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 myabs(x): | |
if x > 0: | |
return x | |
elif x == 0: | |
return x | |
elif x < 0: | |
return x |
This file contains 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 myabs(x): | |
if x < 0: | |
return -x | |
else: | |
return x |
This file contains 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 myabs(x): | |
return -x if x < 0 else x |
This file contains 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 sqrt(x): | |
return sqrt_iter(1.0, x) |
This file contains 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 sqrt_iter(guess, x): | |
if is_good_enough(guess, x): | |
return guess | |
else: | |
return sqrt_iter(improve(guess, x), x) |
This file contains 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
square(2 + 5) | |
square(square(7 + square (3))) |
This file contains 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 square(x): | |
return x * x |
This file contains 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_of_squares(x, y): | |
return square(x) + square(y) | |
sum_of_squares(3, 4) | |
def f(a): | |
return sum_of_squares(a + 1, a * 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment