I'm trying to generate an exception for an infinite loop while folding a map.
module X=Map.Make(Int)
The problem goes like this: Determine the max rank for which the function "f" doesn't lead into an infinite loop. We have a map [(1,2);(2,3);(5,6);(6,7);(7,6)]
The function goes through all the pairs of the map like this:
for the first pair (1,2) => f(1)=2 Now I'll try f(2) = 3 (As it exists in the pair (2,3)) now I'll try f(3) => Cannot find any key = 3 so the rank is the number of times I could call the function f, in this case, it's 2.
for the pair (5,6) => f(5)=6 I'll try f(6) = 7 (the pair (6,7) exists) I'll try f(7) = 6 (The pair (7,6) exists) I'll try f(6) = 7 goes into a loop, because I have already used the pair (6,7) once, so the rank in this case is 0;
Conclusion: The max rank of the function f if it does not go into an infinite loop is 2.
How can I define my exception, as well as what reasoning can I use to solve the problem?