Re: ord_select ?
"Richard A. O'Keefe" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 13/02/2014, at 12:07 AM, Nicos Angelopoulos wrote:
>
> Dear all,
>
> in library library(ordsets) I would like to have the same behaviour as select/3 for lists,
> in that if the element is not in the set the predicate should fail.
select(X, L, R) <->
append(A, Z, R) & append(A, [X|Z], L).
In the ?X, +L, -R mode, this works perfectly for
ordered sets. In the +X, -L, +R mode it doesn't.
> ord_del_element/3 is the closest thing as far as i can see,
> but it does not fail if the element is not in the set.
That's because x ∉ S ⇒ S \ {x} = S
and S \ {x} is what ord_del_element is supposed to compute.
> 1. am i missing the correct predicate to achieve this?
> 2. would it make sense to include some such like predicate to the library?
In Quintus Prolog, library(ordsets) provides this predicate.
The actual code is unrolled for speed; the code here is newly
written.
% ord_selectchk(Item, Set1, Set2)
% is true when select(Item, Set1, Set2) and Set1, Set2 are
% both sorted lists without duplicates. This implementation
% is only expected to work for Item ground and either Set1 or
% Set2 ground. The "chk" suffix is meant to remind you of
% memberchk/2, which also expects its first argument to be ground.
% ord_selectchk(X, S, T) =>
% ord_memberchk(X, S) & \+ ord_memberchk(X, T).
ord_selectchk(Item, [X|Set1], [X|Set2]) :-
X @< Item,
!,
ord_selectchk(Item, Set1, Set2).
ord_selectchk(Item, [Item|Set1], Set1) :-
( Set1 == [] -> true
; Set1 = [Y|_] -> Item @< Y
).
_______________________________________________
SWI-Prolog mailing list
[email protected]
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog