Martin Kunze
Martin Kunze

Reputation: 1045

Prolog - Save List into something like an atom?

I made some predicate, which saves a very complex list into a variable P. Let's say:

  pred(P) :- 
    P = [[1, a, b, c, 234],d].

Now I want to put this list into another predicate, let's say

  pred2(P, L) :-
    nth1(2, P, L).

My problem now is, I don't want to copy [[1, a, b, c, 234], d] as parameter into P. Is their a simple way, maybe to define an atom or another shortcut, e.g. 'test' := [[1, a, b, c, 234],d]. and call ?- pred2('test', L)?

Upvotes: 1

Views: 88

Answers (1)

Will Ness
Will Ness

Reputation: 71065

The simple way is to call the predicate which you've defined:

   pred2(L) :-
     pred(P),    % :- P = [[1, a, b, c, 234],d].
     nth1(2, P, L).

This will "set" nay instantiate the logical variable P first, then use it to "set" L.

Note P has become an internal variable here. You don't have to set it when calling pred2. pred2 will call pred for you, to set P.

Upvotes: 3

Related Questions