home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / cplus / 1548 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  2.2 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!caen!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!boo!uttsbbs!john.stephens
  2. From: john.stephens@uttsbbs.uucp (John Stephens) 
  3. Newsgroups: comp.std.c++
  4. Subject: NAMELESS FUNCTIONS
  5. Message-ID: <667.22.uupcb@uttsbbs.uucp>
  6. Date: 11 Nov 92 18:59:00 GMT
  7. Distribution: world
  8. Organization: The Transfer Station BBS, Danville, CA - 510-837-4610/837-5591
  9. Reply-To: john.stephens@uttsbbs.uucp (John Stephens) 
  10. Lines: 44
  11.  
  12. I'm trying to write a function that accepts a one variable math function
  13. (double argument, double return type) and returns another math function
  14. which will calculate the derivative of the original function argument.
  15. In other words, the following will be possible:
  16.  
  17.         double (*dsqrt)(double) = derivative(sqrt);
  18.         cout << dsqrt(x);
  19.  
  20. The main problem I am running into is that it seems to be impossible for
  21. the returned function to know what function calculate the derivative
  22. for because it only accepts one argument, and the derivative function
  23. has no way of setting the function inside the calculating function!
  24. (Does anyone understand what I'm trying to say?)
  25.  
  26. I was thinking of somehow doing it with classes, which would be much
  27. easier. The usage would be as follows:
  28.  
  29.         Function f(sqrt);
  30.         cout << f.derivative(10);
  31.  
  32. Or, of course, I could write a general derivative function that accepts
  33. a one variable math function and a double value and just return the
  34. derivative at that point. But I would still like to be able to do it the
  35. way I stated originally. One thing that would solve the problem would be
  36. a modified version of C/C++ that would allow the following:
  37.  
  38.         double (*derivative(double (*f)(double)))(double)
  39.           {
  40.           double derivCalc(double x)
  41.             {
  42.             ...calculate and return the derivative of f(x)...
  43.             }
  44.           return derivCalc;
  45.           }
  46.  
  47. It would allow functions to be defined within other functions and use
  48. all of their local variables, without having them explicitly entered as
  49. arguments. In other words, each derivCalc returned could access a
  50. different version of f. Unfortunately I think this is impossible in C
  51. and C++, but who knows.
  52.  
  53. John Stephens
  54. john.stephens@uttsbbs.uucp
  55.                    
  56.