Tak (function)


In computer science, the Tak function is a recursive function, named after Ikuo Takeuchi. It is defined as follows:

def tak
if y < x
tak,
tak,
tak
)
else
z
end
end

This function is often used as a benchmark for languages with optimization for recursion.

tak() vs. tarai()

The original definition by Takeuchi was as follows:

def tarai
if y < x
tarai,
tarai,
tarai
)
else
y # not z!
end
end

tarai is short for tarai mawashi, "to pass around" in Japanese.
John McCarthy named this function tak after Takeuchi.
However, in certain later references, the y somehow got turned into the z.
This is a small, but significant difference because the original version benefits significantly by lazy evaluation.
Though written in exactly the same manner as others, the Haskell code below runs much faster.

tarai :: Int -> Int -> Int -> Int
tarai x y z
| x <= y = y
| otherwise = tarai



You can easily accelerate this function via memoization yet lazy evaluation still wins.
The best known way to optimize tarai is to use mutually recursive helper function as follows.

def laziest_tarai
unless y < x
y
else
laziest_tarai,
tarai,
tarai
end
end
def tarai
unless y < x
y
else
laziest_tarai,
tarai,
z-1, x, y)
end
end

Here is an efficient implementation of tarai in C:

int tarai

Note the additional check for before z is evaluated, avoiding unnecessary recursive evaluation.