Higher order function ? Just more substitution !
Your source code is a long text template, no magic to it.
Higher order function ? Just more substitution !
Your source code is a long text template, no magic to it.
>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.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.
What you’re showing is lazy evaluation which is an alien concept in Python land.
4 → square_root → triple → ?
How does the square_root function work? It's an interesting question but often times assembling multiple atoms into a structure is more empowering and exciting; plus children will be using functions where the details elude them for quite awhile. Using functions with elusive details is also typical in math pedagogy so children should get used to reasoning around black boxes.
Would like to have a word with you.
I have tried to teach that and students have always (seem) to like & understand this approach. It was for student at university level, so maybe more able to understand this 'functional' view of functions.
Dictionaries are more like functions and indeed I taught myself to understand hashmaps as "frozen functions" way back when. You can easily find the set-valued inverse of a function by saying
{v: [k for k in d.keys() if k[v]==v] for k, v in d.items()}
and that's close to the mathematical definition too.
What? In mathematics, you often have things like “let c be a real number; let f(x) = x + c”.
The problem is that it gets very complicated real fast once you start dealing with non-trivial expressions as arguments, shadowed identifiers, and recursion.