list - Prolog : Recursive Function Branching & "Returning" -
I was predicting some recurring Prolog and ran into a certain point which I did not quite understand at this time.
For example, I wrote a bill split / 3
that divides a list of integers into a list of non-negative integers and the list of negative integers: < / P>
version 1
partition ([], [], []). Split ([ListHead | ListTail], [List Head | Plist], NLIIS): - Listside & gt; = 0, Partition (Listlist, Pllist, NLIIT) Partition ([ListHead | ListTail], PList, [ListHead | NList]): - ListHead & lt; 0, Partition (Listal, Plist, NLIIT) But before reaching that solution, I wrote the solution below and wondered why it was not working: Version 2 split ([], [], []). Partition ([ListHead | ListTail], PList, NList): - ListHead & gt; = 0, Partition (ListTail, [ListHead | PList], NList, Partition ([ListHead | ListTail], Plist, NLIIS): - List Head & lt; 0, Partition (Listlist, Plist, [List Head | NLIIS]). Where: - The argument given earlier is divided into
ListHead
and ListTail
. - If the
ListHead
element (integer) is greater than or equal to 0, then it is linked to the list of non-negative integers, and for a recursive call with an uncertified call Logic is used list
. - If the
ListHead
element is less than 0, then it is linked to the negative integer list and used as a logic for recursive an unverified < Call with code> PList
. I can not find why version 2 does not work; It compiles without any warning, but only once is a false one. The only difference with the above version is that the Nlist
or PList
is defined in the predefined definition of the integer elements (: -
operator) , And not in the parameter call for me predictably, it is understood in the preparation of the result as part of the argument for the next call ...
I think I have a protocol Something is missing about the "re-search" call!
What can this do? Does not work because you are losing the elements while back to back tracking.
In version 1, when the return starts, you start to pile up the elements like this:
[ListHead | PList] as [ListHead | []] At the first level,
Finally you have all the lists.
In version 2, the plast remains irreversible and the harvesting condition is never satisfied, because you have something like:
[[]] 1,2 , 3,4,5,6]
and nothing matched.
In the version 2, you need to use the credentials (or auxiliary variable) at the end, which you need to copy into the actual variables of the depositors:
Split ([], a, b, a, b) Partition ([ListHead | ListTail], PList, NList, PListAcum, NListAcum): - ListHead & gt; = 0, Partition (ListTail, PList, NList, [ListHead | PlistAcum], NListAcum) Partition ([ListHead | ListTail], PList, NList, PListAcum, NListAcum): - ListHead & lt; 0, Partition (Listal, Plist, NLIIST, Pleistectum, [List Head | NLISTAQ]).
You say it like this:
split ([1,2,3, -1, -2, -3], p, n , [], []);
Let me explain that the creditor is started and they submit your data. The first line only replicates the depositors in the actual variable, when the list is empty, the cacheists have lost their elements (you will see that you see the names of the variables at different back tracking levels), but the actual variable remains Is unchanged via back tracking.
You will need a container for each variable that you want to return as a result, or prefer it to your first version.
You can find information about it depositors.
Greetings.
Comments
Post a Comment