Discussions related to Visual Prolog
daveplummermd
VIP Member
Posts: 63
Joined: 18 Jul 2006 17:18

Example for removeAllBy

Unread post by daveplummermd »

Folks, could anyone provide an example of how to use "removeAllBy". There is no such example in the programs provided under examples and I don't understand the arguments that need to be included.

Thanks in advance
Dave Plummer MD
Dave Plummer
User avatar
Gukalov
Active Member
Posts: 33
Joined: 5 Oct 2011 15:16

Re: Example for removeAllBy

Unread post by Gukalov »

Code: Select all

implement main     open core   clauses     run() :-         X = "abc",         List = [ "ABC", "aBc", "cba", "abc", "AbC", "abC" ],         L = list::removeAllBy(string::compareIgnoreCase, List, X), %        L = list::removeAllEq({(A, B) :- equal = string::compareIgnoreCase(A,B)}, List, X), %        L = list::filter(List, {(A) :- equal <> string::compareIgnoreCase(A,X)}),         console::write(L),         _ = console::readLine().   end implement main   goal     console::runUtf8(main::run).
daveplummermd
VIP Member
Posts: 63
Joined: 18 Jul 2006 17:18

Re: Example for removeAllBy

Unread post by daveplummermd »

Thank you for the example.
There is some stuff here I don't understand but I'll work through it using debug to learn.
Again, thanks for your reply.
Dave Plummer
daveplummermd
VIP Member
Posts: 63
Joined: 18 Jul 2006 17:18

Re: Example for removeAllBy

Unread post by daveplummermd »

Gukalov

is it possible to do this same thing with different domains?
For example if x=1 and List=[1,2,3] in your example? What is the comparator to be used?

Code: Select all

X=1, List=[1,2,3], L = list::removeAllBy(somecomparitor, List, X),
dave
Dave Plummer
User avatar
Gukalov
Active Member
Posts: 33
Joined: 5 Oct 2011 15:16

Re: Example for removeAllBy

Unread post by Gukalov »

Code: Select all

class predicates     somecomparItor : core::comparator{PofigChyo}. % define your own domain here instead of "PofigChyo" clauses     somecomparItor(A, B) = less :- A < B, !. % define your own "less" here instead of "A < B,".     somecomparItor(A, B) = greater :- A > B, !. % define your own "greater" here instead of "A > B,".     somecomparItor(_, _) = equal.

I think that using list::removeAllBy is not the best way to clean the lists.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1232
Joined: 28 Feb 2000 0:01

Re: Example for removeAllBy

Unread post by Thomas Linder Puls »

The predicate removeAllBy is used when you want to supply your own comparison routine.

For strings it could for example (as above) be a comparison that consider uppercase and lowercase letters the same.

But for numbers it would (almost always) be natural to use the normal number comparison.

Code: Select all

clauses     run() :-         L1 = [1, 2, 3, 4, 5],         L2 = list::removeAllBy(compare, L1, 4),         stdio::writef("L2 = %\n", L2).
However, that is exactly what the predicate removeAll does:

Code: Select all

clauses     run() :-         L1 = [1, 2, 3, 4, 5],         L2 = list::removeAll(L1, 4),         stdio::writef("L2 = %\n", L2).
Regards Thomas Linder Puls
PDC
Post Reply