Search code examples
stringprologswi-prologquotation-markssingle-quotes

Remove single quotes/quotation marks in Prolog


I have an extern API sending info to my Prolog application and I found a problem creating my facts.

When the information received is extensive, Prolog automatically adds ' (single quotes) to that info.

Example: with the data received, the fact I create is:

object(ObjectID,ObjectName,'[(1,09:00,12:00),(2,10:00,12:00)]',anotherID)

The fact I would like to create is

object(ObjectID,ObjectName,[(1,09:00,12:00),(2,10:00,12:00)] ,anotherID)

without the ' before the list.

Does anyone know how to solve this problem? With a predicate that receives '[(1,09:00,12:00),(2,10:00,12:00)]' and returns [(1,09:00,12:00),(2,10:00,12:00)]?


Solution

  • What you see is an atom, and you want to convert it to a term I think.

    If you use , you can use the builtin term_to_atom/2:

    True if Atom describes a term that unifies with Term. When Atom is instantiated, Atom is parsed and the result unified with Term.

    Example:

    ?- term_to_atom(X,'[(1,09:00,12:00),(2,10:00,12:00)]').
    X = [ (1, 9:0, 12:0), (2, 10:0, 12:0)].
    

    So at the right hand side, you enter the atom, at the left side the "equivalent" term. Mind however that for instance 00 is interpreted as a number and thus is equal to 0, this can be unintended behavior.

    You can thus translate the predicate as:

    translate(object(A,B,C,D),object(A,B,CT,D)) :-
        term_to_atom(CT,C).
    

    Since you do not fully specify how you get this data, it is unknown to me how you will convert it. But the above way will probably be of some help.