Tail Calls

  • A tail call happens when a function makes a function call in its return
    • No other work must be required after the function returns
      • return theFunction(x, y, z) would be a proper tail call
      • return theFunction(x, y, z) + 1 would not be a proper tail call
  • This can be used for recursion when it function calls itself in its return
  • It can be used to return the result of another function

In computer science, currying is the technique of transforming a function taking multiple arguments into a function that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result.

  • Reference Curried Lua

  • By the use of currying a function with several parameters can be modified

    • Into several functions with single parameters

The following is an example of currying to reduce the number of parameters per function

function test1 (x, y, z) -- add three number with three parameters return x + y + z end print (test1(5, 4, 3)) function f1 (x) -- add three numbers with one parameter per call, curried return function (y) return function (z) return x + y + z end end end f2 = f1(5) -- calling the curried functions f3 = f2(4) print (f3(3))

References