Example code in ā€œFunctional Programmingā€ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Example code in ā€œFunctional Programmingā€

I donā€™t get this code. Why does the answer come out to be 20? If you pass the value from the first function to the second function, you donā€™t get an answer of 20! Can someone please explain this to me?

28th Jan 2019, 12:53 AM
Fred Jones
Fred Jones - avatar
5 Respostas
+ 2
It does return 15 from the inner function: add_five(add_five(10)) == add_five(10+5) == add_five(15) == 15+5 == 20.
28th Jan 2019, 4:22 AM
Diego
Diego - avatar
+ 1
Are you talking about the code in the first slide? https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2459/
28th Jan 2019, 1:56 AM
Diego
Diego - avatar
0
yes... shouldnā€™t this return 15 from the second funtion and then twice that, from the first function, for a total of 30? what am i getting wrong?
28th Jan 2019, 4:16 AM
Fred Jones
Fred Jones - avatar
0
got that... what about ā€˜apply_twiceā€™ ? doesnt that function double the value returned from ā€˜add_fiveā€™? seems to me that that would be 30...
28th Jan 2019, 4:27 AM
Fred Jones
Fred Jones - avatar
0
You can do what's called substitution. apply_twice is defined as `func(func(arg))`. But then, when we call apply_twice, you can in your head substitute the arguments: `func` becomes `add_five` and `arg` becomes `10`. So `func(func(arg))` becomes `add_five(add_five(10))` and then we are at where Diego Acero started off.
28th Jan 2019, 4:54 AM
Schindlabua
Schindlabua - avatar