Search code examples
prologswi-prolog

What is the equivalent of sprintf in Prolog?


Prolog has an equivalent of the C library printf, which is format/2 or format/3.

But what is the equivalent of sprintf ?


Solution

  • You can use format/3 providing string(S) or atom(A) as the first argument:

    ?- format(string(String), 'Hello ~w', ['World!']).
    String = "Hello World!".
    
    ?- format(atom(String), 'Hello ~w', ['World!']).
    String = 'Hello World!'.