Ell
Ell

Reputation: 101

SWI prolog make set of variables name with rbtrees or others means

I have got a term from which I want to get set of variables name.

Eg. input: my_m(aa,b,B,C,max(D,C),D) output: [B,C,D] (no need to be ordered as order of appearance in input) (That would call like set_variable_name(Input,Output).)

I can simply get [B,C,D,C,D] from the input, but don't know how to implement set (only one appearance in output). I've tried something like storing in rbtrees but that failed, because of

only_one([],T,T) :- !.
only_one([X|XS],B,C) :- rb_in(X,X,B), !, only_one(XS,B,C).
only_one([X|XS],B,C) :- rb_insert(B,X,X,U), only_one(XS,U,C).

it returns tree with only one node and unification like B=C, C=D.... I think I get it why - because of unification of X while questioning rb_in(..).

So, how to store only once that name of variable? Or is that fundamentally wrong idea because we are using logic programming? If you want to know why I need this, it's because we are asked to implement A* algorithm in Prolog and this is one part of making search space.

Upvotes: 0

Views: 234

Answers (1)

mat
mat

Reputation: 40768

You can use sort/2, which also removes duplicates.

Upvotes: 2

Related Questions