0
functional programming
'''please explain how it works''' #FUNCTIONAL PROGRAMMING def apply_twice(func,arg): return func(func(arg)) def add_five(x): return x+6 print(apply_twice(add_five,10))
2 Answers
+ 3
add_five(x) adds 6 (!?) to x and returns the sum.
test: print(add_five(4)) # output: 10
apply_twice takes two arguments, func and arg and then returns func(func(arg)).
If you call apply_twice with the arguments add_five and 10, this is what happens in apply_twice:
return func(func(arg)) => this is the same as return add_five(add_five(10)).
=> add_five(10) is 16.
=> add_five(add_five(10)) equals add_five(16).
=> add_five(16) is 22 and that's the return value of the function.
+ 1
Thanks