+ 3

I need help understanding what is going on with this recursion problem

Could some one explain to me how does this code out put 26 def decor(a,b): def sq(func): return func(a,b)*func(a,b) return sq def subs(a,b): return(a-b) def add(a,b): return (a+b) f = decor(2,3) print(f(add)+f(subs))

13th Mar 2018, 4:27 AM
Deen Lu
Deen Lu - avatar
6 Answers
+ 7
the function decor builds a function sq. a,b --> sq(func) f is f(func): return func(2,3)*func(2,3) f is called twice: f(add) returns add(2,3)*add(2,3) = 5*5 f(subs) returns subs(2,3) * subs(2,3) =(-1)*(-1) =1
13th Mar 2018, 4:59 AM
Oma Falk
Oma Falk - avatar
+ 4
(2+3)**2 + (2-3)**2
13th Mar 2018, 4:37 AM
Oma Falk
Oma Falk - avatar
+ 4
yes
13th Mar 2018, 5:22 AM
Oma Falk
Oma Falk - avatar
+ 2
Line 3 outputs func**2. It's the same as func*func.
13th Mar 2018, 8:11 AM
Chris
Chris - avatar
+ 1
I see ... is it because sq(func) = func**2
13th Mar 2018, 4:45 AM
Deen Lu
Deen Lu - avatar
0
@oma Falk Could you please explain to me which part of the code that outputs **2?
13th Mar 2018, 4:40 AM
Deen Lu
Deen Lu - avatar