Re: ord_select ?
"Nicos Angelopoulos" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <20140212164344.25350c16@ampelos> |
Dear Michael, all, On Wed, 12 Feb 2014 08:07:19 -0700 Michael Hendricks <[email protected]> wrote: > On Wed, Feb 12, 2014 at 4:07 AM, Nicos Angelopoulos < > [email protected]> wrote: > > > 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. > > ... > > 1. am i missing the correct predicate to achieve this ? > > > > Since ordsets are implemented as lists, you should be able to use select/3 > directly. > this doesn't take advantage of the fact that the list is a set writing the predicate myself is not the issue > > > 2. would it make sense to include some such like predicate to the library? > > > > That seems reasonable to me. For large ordsets, a specialized version of > select/3 could get better performance by utilizing the sort order. > the (one) impementation is pretty simple, (see end of message). point (a) is whether it is a useful enough addition to the library- provided it doesn't already exist and (b) what should the name be ord_select/3 or something along the the lines of ord_del_element_?/3 ord_sel_element/3 ? There is also the issue of whether the doc for ord_del_element/3 should explicitly mention that the predicate does not fail in that scenario. The manual does say that This is the same as ord_subtract(Set, [Element], NewSet). which is certainly a warning in the right direction. Regards, Nicos Angelopoulos --- http://stoics.org.uk/~nicos/ %% ord_select(+Set, +Element, -Rem). % % Succeeds iff Elem is in Set and Rem is the set minus this Element. % Very similar to ord_del_element/3 but fails if Element is not in the Set. % % == % ord_select([a,b,c], a, BC). % BC = [b, c]. % % ?- ord_select([a,b,c], d, ABC). % false. % % ?- ord_del_element([a,b,c], d, ABC). % ABC = [a, b, c]. % == % % @version 0.1 2014/02/12 % ord_select([H|T], V, Rem) :- compare(Rel, H, V), ord_select_rel(Rel, H, T, V, Rem). ord_select_rel(<, H, T, V, [H|Rem]) :- ord_select(T, V, Rem ). % ord_select_rel(>, H, T, V, [H|Rem]) :- fail. ord_select_rel(=, _H, T, _V, T).