Created
January 23, 2013 06:45
-
-
Save KGZM/4602668 to your computer and use it in GitHub Desktop.
Examples From Structure and Interpretation of Computer Programs..
I decided to go through and make some of my previously private gists into public ones.
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
inc = function(x) { | |
return x + 1; | |
}; | |
square = function(x) { | |
return x * x; | |
}; | |
cube = function(x) { | |
return x * x * x; | |
}; | |
deriv = function(g) { | |
var dx; | |
dx = 0.00001; | |
return function(x) { | |
return (g(x + dx) - g(x)) / dx; | |
}; | |
}; | |
double = function(f) { | |
return function(x) { | |
return f(f(x)); | |
}; | |
}; | |
compose = function(f, g) { | |
return function(x) { | |
return f(g(x)); | |
}; | |
}; | |
repeated = function(f, n) { | |
return function(x) { | |
if (n === 1) { | |
return f(x); | |
} else { | |
return (compose(f, repeated(f, n - 1)))(x); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment