The following is a function to write a recursive function that accepts an array and a callback. Function returns true if a single value in array returns true when passed to the callback otherwise it returns false.
I have trouble understanding the "if not" line written here(line 4), how does the cb interact with arr? Please help.
def isOdd(num):
if num%2==0:
return False
else:
return True
def someRecursive(arr, cb):
if len arr == 0:
return False
if not(cb(arr[0])): #Maybe till it is an empty array. DOUBT HERE
return someRecursive(arr[1:],cb)
return True
not
yieldsTrue
if its argument is false,False
otherwise. The parentheses aroundcb(arr[0])
are dispensable. The statement inisOdd(num)
might better readreturn num%2;
,return num%2 != 0;
or evenreturn not (num%2 == 0);
.) $\endgroup$cb
is a function, socb(arr[0])
is simply callingcb
witharr[0]
as an argument. What don't you understand? $\endgroup$