Re: Boundary conditions
Alan Baljeu <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
"I am looking for those items in Check that are not member from Control" unique_member(Item, Check, Control) :- % translating your words directly into Prolog: member(Item, Check), \+ member(Item, Control). To get all of them: setof(Item, (unique_member(Item, Check, Control)), Items). Of course if you don't want to use setof,bagof or findall then your approach is okay except for two things: 1) the last clause accidentally is called uniques_List. (That's why it doesn't work.) 2) Clause 3 overlaps with clause 2, leaving a choice point. This means your answer would be all of these: U = [6, 5] ; U = [5] ; U = [6] ; U = [] ; false. You probably only want the first. Inserting a ! after the \+ member(H, Control) will solve this. Alan Baljeu ________________________________ From: Bengbers <[email protected]> To: [email protected] Sent: Wednesday, November 13, 2013 12:46:24 PM Subject: [SWIPL] Boundary conditions I am a novice to Prolog so please excuse this maybe stupid question. I have two lists: Control = [1,2,3,4] Check = [4,5,6] and I am looking for those items in Check that are not member from Control ([5,6]) I have written the following code: 1 uniques_list( Control, Check, Unique) :- uniques_list( Control, Check, [], Unique). 2 uniques_list( Control, [H|Tail], Accu, Unique) :- \+member( H, Control), Accu2=[H|Accu], uniques_list( Control, Tail, Accu2, Unique). 3 uniques_list( Control, [_|Tail], Accu, Unique) :- uniques_list( Control, Tail, Accu, Unique). 4 uniques_List( Control, [], Unique, Unique). When calling uniques_list( [1,2,3,4], [ 4,5,6], X) and tracing the code, I see at some moment that Accu contains [5,6] and that the tail is []. I would expect that the flow continues to line 4 and that the call returns but instead it continues with line 2. My questions are: - How can I fix this (and let X unify with [5,6])? - What are the boundary conditions (why doesn't the flow jump to line 4)? Ben -- View this message in context: http://swi-prolog.996271.n3.nabble.com/Boundary-conditions-tp13445.html Sent from the SWI Prolog mailing list archive at Nabble.com. _______________________________________________ SWI-Prolog mailing list [email protected] https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog -------------- next part -------------- HTML attachment scrubbed and removed