Created
September 24, 2011 15:08
-
-
Save Quby/1239433 to your computer and use it in GitHub Desktop.
SICP in javascript
This file contains hidden or 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
| var factorial = (function () { | |
| "use strict"; | |
| function fact (product, counter, x) { | |
| return counter > x ? product : fact(counter * product, counter + 1, x); | |
| } | |
| return function (x) { | |
| return fact(1, 1, x); | |
| }; | |
| })(); |
This file contains hidden or 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
| var sqrt = (function () { | |
| "use strict"; | |
| function sqrt (n, x, p) { | |
| return Math.abs(x*x - n) < p ? x : sqrt(n, (n/x + x) / 2, p); | |
| } | |
| return function (n) { | |
| return sqrt(n, 1, 0.001); | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment