Topic: NAMELESS FUNCTIONS
Author: Danny R Dalal <dd3e+@andrew.cmu.edu>
Date: Tue, 17 Nov 1992 16:04:18 -0500 Raw View
Are you trying to curry fucntions?
If you can generalize it without a high efficiency penalty, that would
be very usefull.
___Dan
Author: john.stephens@uttsbbs.uucp (John Stephens)
Date: 11 Nov 92 18:59:00 GMT Raw View
I'm trying to write a function that accepts a one variable math function
(double argument, double return type) and returns another math function
which will calculate the derivative of the original function argument.
In other words, the following will be possible:
double (*dsqrt)(double) = derivative(sqrt);
cout << dsqrt(x);
The main problem I am running into is that it seems to be impossible for
the returned function to know what function calculate the derivative
for because it only accepts one argument, and the derivative function
has no way of setting the function inside the calculating function!
(Does anyone understand what I'm trying to say?)
I was thinking of somehow doing it with classes, which would be much
easier. The usage would be as follows:
Function f(sqrt);
cout << f.derivative(10);
Or, of course, I could write a general derivative function that accepts
a one variable math function and a double value and just return the
derivative at that point. But I would still like to be able to do it the
way I stated originally. One thing that would solve the problem would be
a modified version of C/C++ that would allow the following:
double (*derivative(double (*f)(double)))(double)
{
double derivCalc(double x)
{
...calculate and return the derivative of f(x)...
}
return derivCalc;
}
It would allow functions to be defined within other functions and use
all of their local variables, without having them explicitly entered as
arguments. In other words, each derivCalc returned could access a
different version of f. Unfortunately I think this is impossible in C
and C++, but who knows.
John Stephens
john.stephens@uttsbbs.uucp