4 ms·
How 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
by ehaliewicz2 8y ago
How 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.