MartyIX
MartyIX

Reputation: 28648

How to concatenate two atoms/strings?

I'm trying to find out how to concatenate two atoms:

A = 'my ', 
B = 'atom',

How can I concatenate these two atoms so that the result is:

'my atom'

?

Upvotes: 29

Views: 34137

Answers (2)

Kaitlin Hipkin
Kaitlin Hipkin

Reputation: 199

Here is a simple solution that doesn't overcomplicate things.

To append atoms, use the atom_concat/3 predicate:

A='my ', 
B='atom', 
atom_concat(A, B, FinalAtom).

To append strings, use the string_concat/3 predicate:

A="my ",     % single quotes also work
B="string", 
string_concat(A, B, FinalString)

Both links go to pages on the official SWI-Prolog documentation website.

Upvotes: 15

MartyIX
MartyIX

Reputation: 28648

For atoms:

?- atom_concat('my ', 'atom', X).
X = 'my atom'.

For strings:

:- set_prolog_flag(double_quotes, chars).
:- use_module(library(double_quotes)).

?- append("my ", "string", X).
X = "my string".

It took me a while to find the proper names. Maybe it will help others too.

Upvotes: 29

Related Questions