0
def apply_twice(func,arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five,10)) #output:20
Functional programming, please someone should please take time and explain it for me
6 Answers
+ 1
add_five function returns
x + 5 (if you input 5, it returns 10)
apply_twice function returns
add_five(add_five(10))
= add_five(15)
= 20
0
To build on what's been said, the return type of the function parameter and the data type of the second parameter should probably be the same for âapply_twice()â to work if I'm not mistaken
0
RUSIRU "if you input 5"? Where do I get the 5 from?
0
Carleaf It's just a example. if it is 10, return will be 15. add_five function adds 5 to whatever the value you pass into the function.
0
When you call apply_twice(add_five,10)
The code execute func(func(arg))
First execute func(arg)
Then result take place of arg
is the same
variable=func(arg) //in your case arg is 10, then it execute add_five that return arg+5
func(variable) //variable is arg+5, then your execute again the function add_five, then your return arg+5+5=10+5+5=20