prolog - Stop recursion when variable is reached -


I have a program with searches for something and it has not been found every time, it increases a variable by 1 is. If it never detects what the user is searching for, he will always search for it. Can I stop doing so, if any particular variable has reached? This is the Victkit that searches for:

 % ----------------------------- - ---------------------------------------% address (X, Y, N): - ... Search for% (X, Y, N): - ... if not found, increment n 1 and repeat it again% ------------------ -------------------------------------------------- - Address (Y, I, G): - Member (Y, I), G. Find (Y, I, G): - Find (Member (Y, I)), Extension (I, O), Find (Y, O, G1), GG1 + 1 Find (Y, I, 50 ): - Failure  

Then I have to write something

  Find (X, Y, 50): - Return false  

So that the program returns to false, if it is not found after 50 iterations. How do I know this?

Edit: This is my code:

vwg (x, y, g) searching that two person x and for example G-grade Is related to:

  vwg (vanessaMueller, selinaMueller, 1)  

The truth is, Vanesses Mueller is her mother

If you always have the third argument (recurrence number) bound, then you can simply count down. Once you hit zero, you have failed:

Find (Y, I, G): -% Integer (G),% G & gt; 0,% If we have not yet zeroed in, then the member (Y, I),% see that we can get in Y! . % And eliminate any options. Find (Y, I, G): -% integer (g),% apply the contract that should be bound to G & gt; 0,% If we have not yet zeroed to G1, then G -1,% decreases the expansion of G (I, I1),% expansion I find (Y, I1, G1)% and revised below. %

Note that the initial requirement for the requirement above find / 3 is bound to its third argument integer is.

Instead, instead of defining your limit to return , (and instead of using a hard-coded limit instead) So you find an accumulator with a helper:

  Find (Y, I, G): - Find (Y, I, 1, G). Find (Y, I, G, G): - G = & lt; 50,% If we have not crossed the boundary member (Y, I) so far, then we can meet in Y! . % And options are over (Y, I, T, G): -% T & lt; 50,% If we are below the threshold, then T1, T + 1, expansion of container (I, I1)% increase,% extension (Y, I1, T1, G) % easy!  

Or you can pass another argument in the range and get a recurring calculation on success:

  find (Y, I, n , G): - Find (Y, I, N, 1, G). Find (Y, I, N, G, G): - G = & lt; N, Member (Y, I),! . Find (Y, I, N, T, G): - T & LT; Find N, T1 T + 1, Extension (I, I1), (Y, I1, N, T1, G).  

There is more than one way to do this.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

How to access user directory in lazarus? -

java - Gradle dependencies: compile project by relative path -