[Tutor] nested functions [differentation / C++ comparision]

Cameron Stoner wolf_binary@hotmail.com
Wed, 10 Apr 2002 14:35:18 -0500


I put your test code in and it give me this error:

diffed_square = diff(square)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    diffed_square = diff(square)
NameError: name 'square' is not defined

I don't quite understand how this function in a function thing works with
what you put down for here:

> The above definition is just something I pulled out of a calculus book,
> and is only a numerical approximation.  Still, it should work well enough
> for this example.  Let's see how it works:
>
> ###
> >>> diffed_square = diff(square)
> >>> square(10)
> 100
> >>> diffed_square(10)
> 20.000099999890608
> ###

Square isn't defined so what are you passing into diff?  I am very
interested in this making code on the fly building idea.  It has so many
possibilities.

Cameron Stoner


>
> On Tue, 9 Apr 2002, Pijus Virketis wrote:
>
> > Cameron,
> >
> > >Why would you want to define functions inside functions?  It seems
> > >kinda like a class with methods.  When would you want to do
> > > something like this?
> >
> > Not that I take advantage of this possibility often, but you could have
> > your code generate other code on the fly: it's the big selling point of
> > LISP and its dialects, as far as I know. You recognise some input, and
> > then you create create the right function. The rest of your code can
> > expect "foo()" to be the right tool for the job in the context, no
> > matter what had to go into making it.
>
>
> Here's a math-motivated example.  In Calculus, there's an operation called
> 'differentiation' that works on many mathy functions.  For example, the
> function:
>
>     f(x) = x^2
>
> has the derivative:
>
>     d f(x) = 2x
>    ---
>    dx
>
> (Differentiation tells us that if we take a tangent line to the point, (x,
> f(x)), we'll find the slope is equal to the value of the differentated
> function at that point too.)
>
>
> What's particularly unique about differentiation is that it's main
> target are functions!  That is, differentiation take a function, and
> returns a whole new function.
>
> We can model this in Python --- we can imagine some Python function called
> 'diff' that takes in a function, and returns its derivative.  Here's a
> sample implementation:
>
> ###
> def diff(f, epsilon=0.0001):
>     """We take the function f and creates a whole new function g that
>     approximates the derivative of f."""
>     def g(x):
>         return (f(x+epsilon) - f(x)) / epsilon
>     ## The key step here is to return this new g function!
>     return g
> ###
>
>
> When we make functions that return new functions, we'll find ourselves
> creating an inner function definition, just so we can give it to someone
> else.
>
>

TOOK CODE FROM HERE

>
>
>
> C++ doesn't allow us do do wacky things like writing functions in
> functions.  However, we can can simulate this effect with the judicious
> use of classes.  Here's what the example with differentiation would look
> like in C++:
>
> //////
> #include <iostream>
>
> class Function {
>     // Here, we define that a Function is something that knows how
>     // to evaluate itself at a certain point 'x'.
> public:
>     virtual float eval(float x) = 0;
> };
>
>
> class SquareFunction : public Function {
>     // Here's one particular example of a Function class.
> public:
>     float eval(float x) {
>         return x * x;
>     }
> };
>
>
> class DiffFunction : public Function {
>     // We can differentiate a particular Function instance by using
>     // DifferentiatedFunction.
> public:
>     DiffFunction(Function *f, float epsilon=0.0001) {
>         this->f = f;
>         this->epsilon=epsilon;
>     }
>     float eval(float x) {
>         return (f->eval(x + epsilon) - f->eval(x)) / epsilon;
>     }
>     Function *f;
>     float epsilon;
> };
>
>
> // Small test function
> int main() {
>     cout << "Let's test this out.\n";
>     Function *square = new SquareFunction;
>     Function *diff_square = new DiffFunction(square);
>     cout << square->eval(10) << " is what square(10) gives us.\n";
>     cout << diff_square->eval(10) << "is what diff_square(10) "
>          << "gives us.\n";
>     return 0;
> }
> //////
>
>
> But as you can tell, this is somewhat... painful compared to the Python
> code.  *grin* So it's possible to live without being able to make nested
> functions if we have classes, but the resulting code is just not as
> elegant or straightforward as the nested-function approach.
>
>
> We can give more examples of functions in functions that aren't math
> related.  I just picked the math one because I was reviewing tangents and
> limits last night.
>
> If you have more questions, please feel free to ask!  Good luck!
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>