Mockingbird
Mockingbird

Reputation: 1031

Rename Variables in SWI Prolog

How can I rename variables in SWI Prolog?

I tried to use numbervars predicate like this.

numbervars(Xs,1,_,[functor_name(name)]).

So if Xs is a list of 4 variables, it will look like this when I commit 'writeln(Xs)'.

[name(1),name(2),name(3),name(4)]

How can I use that functor, or any other way to remove the parenthesis and make it looks like:

[name1,name2,name3,name4]

Thanks in advance.

Upvotes: 1

Views: 865

Answers (1)

lurker
lurker

Reputation: 58304

You can write your own subset of numbervars/4 (or completely rewrite your own, if you wish). Here's a subset that performs the specific task you're describing:

build_atoms([Var|VarList], N, Prefix) :-
    atom_number(Atom, N),
    atom_concat(Prefix, Atom, Var),
    N1 is N + 1,
    build_atoms(VarList, N1, Prefix).
build_atoms([], _, _).

This accepts a list of variables and instantiates them in sequence with the atom given in Prefix contactenated with integers, starting with N.

For example:

?- X = [A,B,C], build_atoms(X, 1, foo).
X = [foo1, foo2, foo3],
A = foo1,
B = foo2,
C = foo3.

?-

This can easily be expanded to include any other functionality in numbervars that you desire.

Upvotes: 2

Related Questions