By "splitting a string into atoms" I assume you mean more specifically "splitting a string into a list of characters".
First, we need to distinguish strings versus *atoms. In Prolog, a string as denoted with double quotes is normally a list of character codes, whereas an atom as denoted with single quotes, or a name starting with a lower case letter, is a symbol. See What is the difference between ' and " in Prolog. Here's what it looks like in GNU Prolog:
| ?- X = "abc". % A string of characters
X = [97,98,99]
yes
| ?- X = 'abc'. % An atom
X = abc
yes
| ?- X = abc. % An atom
X = abc
yes
| ?- X = 'Abc'. % An atom
X = 'Abc'
yes
| ?-
So, if you want to get the head and rest of a list of character codes from a string, it's as simple as unifying them:
| ?- "abcd" = [H | T].
H = 97
T = [98,99,100]
yes
| ?-
On the other hand, if you want to do this with an atom, then you need first to convert the atom to character codes using atom_chars/2
:
| ?- atom_chars(abc, [H|T]).
H = a
T = [b,c]
yes
| ?-
In SWI Prolog, they've allowed the use of double quotes to represent an integral string without it representing a list of character codes. It is not, though, the same as an atom. See Why has the representation of double quoted text changed?. Here are some queries in SWI Prolog:
1 ?- "abc" = 'abc'.
false.
2 ?- X = "abc".
X = "abc".
3 ?- "abc" = [H|T].
false.
4 ?- atom_chars("abc", [H|T]).
H = a,
T = [b, c].
5 ?- atom_chars(abc, [H|T]).
H = a,
T = [b, c].
Oddly, "abc"
is not the same as the atom abc
(as seen by the failing unification), but atom_chars/2
treats "abc"
the same as it would the atom, abc
.
Prolog has flags that affect its behavior. In SWI Prolog specifically, there is the double_quotes
flag, whose value can be modified so that the double quote representation behaves in the traditional Prolog sense, as shown in the GNU Prolog example above.
Back to the problem at hand, atom processing predicates are fairly fundamental to Prolog, so it's good to know the basic ones by heart if you need to do atom processing in a closed-book exam.