Puneet Varma (Editor)

Tak (function)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

In computer science, the Tak function is a recursive function, named after Ikuo Takeuchi (竹内郁雄). It is defined as follows:

τ ( x , y , z ) = { τ ( τ ( x 1 , y , z ) , τ ( y 1 , z , x ) , τ ( z 1 , x , y ) ) if  y < x z otherwise

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:

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.

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.

Here is an efficient implementation of tarai() in C:

Note the additional check for (x <= y) before z (the third argument) is evaluated, avoiding unnecessary recursive evaluation.

References

Tak (function) Wikipedia