3 ms·
>Function application is just substitution. >Your source code is a long text template, no magic to it. No, no, no! This is wrong, and really bad pedagogy.
by _dain_ 4y ago
>Function application is just substitution.
>Your source code is a long text template, no magic to it.
No, no, no! This is wrong, and really bad pedagogy.
def func_1(x):
return x + x
def func_2(y):
print(y)
return y
z = func_1(func_2(5))
If function application is substitution, then a student may expect the expansion to work something like this:
z = func_1(func_2(5))
z = func_2(5) + func_2(5)
z = { print(5); 5 } + { print(5); 5 } # pretend python has curly braces for this to make sense
so 5 would get printed twice. But in reality it only gets printed once.
Doing the substitution from inside-out doesn't help either, you get the same thing:
z = func_1(func_2(5))
z = func_1({ print(5); 5 })
z = { print(5); 5 } + { print(5); 5 }
I've had to un-teach this mistaken idea in beginners before. It harms their understanding of the call stack, scoping rules, etc.
- lo_zamoyski 4y ago> Function application is just substitution. >> No, no, no! This is wrong, and really bad pedagogy. I would avoid the word "substitution" until after evaluation order has been covered. Python has applicative order evaluation like most languages. Pedagogically, since evaluation is just a basic concept, it should be explained before the student even gets to functions. Then, when you get to function application, it probably shouldn't take much more to explain that arguments are evaluated first and only then is the function applied on the evaluated values of those arguments. The values are then substituted wherever the parameter appears. If side effects weren't in the picture, you could postpone discussion of evaluation order a little bit longer until you discuss the consequences evaluation rules have for runtime complexity (using pedagogically appropriate language, of course). So you could get away with allowing inaccurate ideas about substitution for a little while.
- xigoi 4y agoIt's unfortunate that Python calls procedures “functions”, which is mathematically wrong and leads to this mistake.
- shrimpx 4y agoIf you’ve covered eager evaluation, the substitution analogy mostly works. What you’re showing is lazy evaluation which is an alien concept in Python land.
- deleted 4y ago[deleted]