Re: Boundary conditions

Bengbers <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
> and I am looking for those items in Check that are not member from Control

wanted_member(X, Check, Control) :-
    member(X, Check),
    \+ memberchk(X, Control).

If you are sure that the lists will always be sorted,

all_wanted_members(Check, Control, Wanted) :-
    ord_subtract(Check, Control, Wanted).

*By mail, someone suggested the use from library ordsets. I had already
found ord_subtract :-)*

> I have written the following code:

Hang on a minute, you said that you want elements that ARE in Check
and are NOT in Control.  *Nothing* in that says anything about any
kind of uniqueness.  So where does "uniques" come from?

*I agree that the name 'uniques' is confusing. 'New' would have been
better.*

Why on _earth_ do you put spaces after left parentheses?

*I remember having read somewhere that this was the preferred coding-style
in Prolog...*

The word "_list" here doesn't add any information either.

*Both 'Control' and 'Check' are the results of setof/3 so they contain no
duplicates. 'Unique' is the list that results from subtracting 'control'
from 'Check'.*

>
> 2 uniques_list( Control, [H|Tail], Accu, Unique) :-
>    \+member( H, Control), Accu2=[H|Accu], uniques_list( Control, Tail,
> Accu2, Unique).

Why have you switched from the name "Check" to the name "Tail"?
This is confusing?
"Accu" is a state of Unique;
the standard naming convention here would be

        uniques_list(Control, [X|Check], Unique0, Unique) :-
            \+ member(X, Control),
            !, % you need this so the next clause isn't tried.
            Unique0 = [X|Unique1],
            uniques_list(Control, Check, Unique1, Unique).

*The theory (and Alan Baljeu) say that the cut is needed here but even
without the !, my procedure now works fine. Is this just plain luck?*

Oh whoops.  I see that for no apparent reason you are reversing
the list Check as you filter it to Unique.  Why bother?

*The order is not relevant to me.*

> 3 uniques_list( Control, [_|Tail], Accu, Unique) :-
>    uniques_list( Control, Tail, Accu, Unique).
> 4 uniques_List( Control, [], Unique, Unique).

Here you switched from calling the 3rd parameter Accu to
calling it Unique.  The names in an accumulator pair (or list
difference pair) should have a common prefix.

Let's rewrite this:

subtract([X|Xs], Ys, Zs) :-
    memberchk(X, Ys),
    !,
    subtract(Xs, Ys, Zs).
subtract([X|Xs], Ys, [X|Zs]) :-
    subtract(Xs, Ys, Zs).
subtract([], _, []).

uniques_list(Control, Check, Unique) :-
    subtract(Check, Control, Unique).

The subtract/3 predicate is in the DEC-10 Prolog
library file SETS.PL, the Quintus library(sets) module.

This will take O(|Control| * |Check|) time; if you can
ensure the lists are ordered it is better to use ord_subtract/3
 as that takes O(|Control| + |Check|) time. 

*Thanks a lot for all the remarks.
Apart from the corrections you also give a lot of suggestions for the coding
style. For better readability, I prefer using names instead of characters
but that is just a naming convention.
On internet I see that there exist a lot of coding styleguides for Prolog.
Which coding convention is preferred by most of the serious
Prolog-programmers?

Ben*



--
View this message in context: http://swi-prolog.996271.n3.nabble.com/Boundary-conditions-tp13445p13455.html
Sent from the SWI Prolog mailing list archive at Nabble.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.