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

13th Oct 2021, 5:55 PM
Khalif Baby👶
Khalif Baby👶 - avatar
6 ответов
+ 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
13th Oct 2021, 6:10 PM
Rusiru Rathmina
Rusiru Rathmina - avatar
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
13th Oct 2021, 6:31 PM
Tim
Tim - avatar
0
RUSIRU "if you input 5"? Where do I get the 5 from?
13th Oct 2021, 9:47 PM
Khalif Baby👶
Khalif Baby👶 - avatar
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.
14th Oct 2021, 2:30 AM
Rusiru Rathmina
Rusiru Rathmina - avatar
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
15th Oct 2021, 4:14 AM
Nyashi