Skip to content

Instantly share code, notes, and snippets.

@KGZM
Created January 23, 2013 06:45
Show Gist options
  • Save KGZM/4602668 to your computer and use it in GitHub Desktop.
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.
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