3 ms·
All this could be done easily in C (function pointers, factorial for loop etc.). And sampling a function twice at 0.0001 dx's apart and calling it "doing calcul
by QuadrupleA 8y ago
All this could be done easily in C (function pointers, factorial for loop etc.). And sampling a function twice at 0.0001 dx's apart and calling it "doing calculus" is a stretch.
The author seems to be making the case that, contrary to his original skepticism, Lisp is indeed some magical higher-order language, but I honestly don't see it. Lisp's ratio of philosophizing to noteworthy projects seems to tend toward infinity.
- jimhefferon 8y agoIt can all be done on a Turing machine. It is a question of tools, and the cultures that build and use those tools, and the interactions between those two things. Like others writing here and like the author of the post I, too, had my imagination fired by SICP and the culture it fits in. When I pick up a Learn to Program book and the examples are managing a used car dealer's inventory, I can only say "Thanks" to Abelson and Sussman. They turned me on to something exciting.
- gameswithgo 8y agomodern c compilers will turn the first example iterative too. but in 1983 this would have seemed magic.;
- pflanze 8y agoMaybe worth noting: when I tested a few years ago at least gcc (don't remember whether I tested clang) only applied TCO to small subprograms. As soon as the iterations were multiply nested and/or across multiple top-level functions (even if all of them in the same compilation unit) it would leave parts of the code still eating stack.
- Jach 8y ago'Peaker pointed out that the GCC optimization involved in the factorial example is actually quite brittle: https://news.ycombinator.com/item?id=15698924 https://news.ycombinator.com/item?id=15698924
- pflanze 8y agoBut the optimization there is rewriting the algorithm (which is 'brittle' because it depends on the operation used being commutative in that example). Just optimizing tail calls as written in the programmer's code (which is what is normally meant with "TCO") don't need such rewriting, and would be trivial to implement via a jump if the back-end supports that, except C also needs to take into consideration structures on the stack, and perhaps other details, hence apparently gcc's handling isn't straight-forward even for plain TCO and fails to be applied in all applicable cases (or at least that's what happened ~4 years ago).
- ehaliewicz2 8y agoHow could you create the equivalent semantics without closures and higher level functions in C? For example, if you wanted to take the second derivative of f (deriv (deriv f)) or if you wanted to take the nth derivative (define (nth-derivative f n) (if (= n 0) f (deriv (nth-derivative f (- n 1)))) You could do it with some data structures, which you'd then have to interpret, but not as nicely or easily.
- QuadrupleA 8y ago#define DX 0.0001 typedef double (*func)(double); double NthDeriv(int n, func f, double x) { if (n == 1) { return (f(x + DX) - f(x)) / DX; } else { return (NthDeriv(n - 1, f, x + DX) - NthDeriv(n - 1, f, x)) / DX; } } double Cube(double x) { return x * x * x; } double result = NthDeriv(3, &Cube, 5.0);
- ehaliewicz2 8y agoTouche. It's not as general as the lisp example though.