Joachim Low
Joachim Low

Reputation: 277

Connect a constant to a variable

Is it possibile associate costant and variable in the same argument? For example:

  expression(N):-
       write(t N),
       N1 is N+1,
       expression(N1).  

where t N become t1, t2, t3... etc. How can I do this?

Upvotes: 2

Views: 71

Answers (1)

lurker
lurker

Reputation: 58244

In SWI prolog:

expression(N) :-
    atom_concat('t', N, TN),  % Note: N must be instantiated in this case
    write(TN),
    N1 is N+1,
    expression(N1).

Interestingly, SWI is happy with this even if N is an integer or an atom (it will treat N as an atom in that case). GNU doesn't like it if N is an integer. So you have to convert it first:

expression(N) :-
    number_atom(N, AtomN),  % Note: N must be instantiated in this case
    atom_concat('t', AtomN, TN),
    write(TN),
    N1 is N+1,
    expression(N1).

Upvotes: 2

Related Questions