If the function is purely functional, that is, it has no state or side effects, then you can save the Set arguments (edit: when you see your editing, you will save the set of pairs (x, y)) with which it was called, and each time simply check if the current argument is in the set. This way you can detect a cycle if you run into it quite quickly. But if the argument space is large, and it takes a lot of time to repeat, you can run out of memory before you discover a loop. In general, of course, you cannot do this because it is a stopping problem.
source share