0

I'm struggling to figure out how do higher order functions work, more specifically folder functions. I ran it through pythontutor a couple of times, however I don't quite understand how to trace the code correctly.

def fold(op, f, n):
    if n==0:
        return f(0)
    else:
        return op(f(n), fold(op, f, n-1))

fold(lambda x,y:x-y, x, 4)
martineau
  • 119,623
  • 25
  • 170
  • 301
fluxeroth
  • 1
  • 1
  • I think the edit unintentionally changed foldr (right fold) to folder, which is likely not the intended question. Perhaps OP can clarify. – Jon Feb 13 '20 at 19:35
  • See these existing questions in case they are of help to you: https://stackoverflow.com/questions/10366374/what-is-the-pythonic-equivalent-to-the-fold-function-from-functional-program, https://stackoverflow.com/questions/29372983/how-to-write-foldr-right-fold-generator-in-python – Jon Feb 13 '20 at 19:36
  • yep the original function was specifically a foldr function. – fluxeroth Feb 14 '20 at 04:53

0 Answers0