Saturday 15 August 2015

Prolog does not end calculation? -


This is a technical question that I think I am trying to write a program that gives me all the sub-sizes Find out the integer 1,2 K, ..., n.

I asked about the sub-set function I am using. Fixed version is:

  subs (0, [], x). Subs (N, [A | R1], [A | R2]): - N & gt; 0, N1N-1, all (N1, R1, R2). Subs (N, [A | R1], [B | R2]): - N & gt; 0, all (n, [a | r1], r2).  

Later in two sets to help me find sub-set of all the elements except the last element and the last (because [A | rest] A is the first and is the remaining from number 2 to last, but I want the opposite - from the last elements and from the beginning to all the elements first a). The tasks are:

  last (A, [A]) last (a, [b | r]): - the last (a, r) ​​subLast ([], [x]). Substitutes ([A | R1], [A | R2]): - Substant (R1, R2)  

I now wrote a function that first creates a list of natural numbers:

  setOf (0, []) . SetOf (N, Nums): - Previous Off (N, Damp), N & gt; 0, N1N-1, Substant (Neonmys, Knms), set off (N1, Neonom).  

To combine all of the above above: Select

  (K, N, X): - setOf (N, Y), subs (K , X, Y).  

For example, on 2 and 4 , I'm getting:

 ? - Choose (2,4, X) - X = [1, 2]; X = [1, 3]; X = [1, 4]; X = [2, 3]; X = [2, 4]; X = [3, 4]; Revoked% Execution 14? Error: user_input: stream: 6: 143 syntax error: unexpected end of the block  

These are all correct outputs, but the problem is that every time I get the next (potential) next answer Sign in, I am the next one, in the past, in which I have to stop empowered, because it seems that the program is trapped in some kind of infinite loop.

Can someone help?

I am using SWI-Prolog.

setOf Here's the problem more specifically - lastOf < / Code>, which is producing infinite number of possible lists ending with N . Anyway, setOf can be implemented in a very easy and more readable way (and which is ending):

  setOf (0, []) set off (n, [n] | T]): - N & gt; 0, N1N-1, is set off (N1, T).  

This is when you do not care about numbers reverse order. Otherwise, by presenting a supporting bill:

  setOf (N, X): - Category (1, N, X). Category (Lowerbound, Upper Bound, Resultist) Category (L, L, [L]). Category (L, U, [L | T]): - L & LT; U, L1 L + 1, Range (L1, U, T)  

No comments:

Post a Comment