vesii
vesii

Reputation: 3128

Turning a relation into a fact

I would like to ask a very basic question in prolog. I have a list of lists that looks like [[a_1,a],[a_2,c],[a_3,e,f]]. I would like to create a relation which gets that list and adds an infix to each one of those sublists. For example I would like to insert x as infix: x([[a_1,a]),x([a_2,c]),x([a_3,e,f]]). The goal is to make the relation to be a fact. I was trying to use findall but without any success.

Example:

turn([[a_1,a],[a_2,c],[a_3,e,f]]).

Output:

[x([a_1,a]),x([a_2,c]),x([a_3,e,f])].

How to implement it?

Upvotes: 1

Views: 34

Answers (1)

lurker
lurker

Reputation: 58314

[x([a])] is not, in and of itself, a fact. It's just a different Prolog term form. A fact is a term that is asserted in the database, doesn't represent a predicate, and can be queried. Sounds like you don't want to convert to "facts", but you just want to convert to a different term form.

It would be easy in this case with maplist:

turn_item(X, x(X)).

turn(TermList, NewTermList) :-
    maplist(turn_item, TermList, NewTermList).

Then:

| ?- turn([[a_1,a],[a_2,c],[a_3,e,f]], L).

L = [x([a_1,a]),x([a_2,c]),x([a_3,e,f])]

yes

Upvotes: 2

Related Questions