Atoms, Strings, Chars & Codes

Scryer/Trealla default double_quotes = chars ("abc" = [a, b, c]) — elegant with DCGs.

18.1 Representations

hello, 'Hello World' interned, fast comparison compact text object [h, e, l, l, o] ISO-friendly, great for DCGs

Scryer/Trealla default double_quotes = chars ("abc" = [a, b, c]) — elegant with DCGs.

18.2 Atom predicates (ISO)

?- atom_length(hello, L).                    % 5
?- atom_concat(hello, ' world', A).          % 'hello world'
?- atom_concat(X, Y, abc).                   % enumerates splits
?- sub_atom(hello, 1, 3, _, S).              % S = ell
?- sub_atom(hello_world, B, _, 0, world).    % find suffix position
?- atom_chars(hello, Cs).                    % [h, e, l, l, o]
?- atom_codes(hi, Cs).                       % [104, 105]
?- atom_number('42', N).                     % 42
?- char_code(a, C).                          % 97
?- upcase_atom(hello, U).                    % 'HELLO'
?- atomic_list_concat([a, b, c], '-', A).    % 'a-b-c'
?- atomic_list_concat(Parts, ',', 'x,y,z').  % split: [x, y, z]
?- term_to_atom(foo(X), A).

18.3 String predicates (SWI)

?- string_concat("ab", "cd", S).
?- string_length("hello", L).
?- sub_string("hello world", 6, 5, _, S).        % "world"
?- split_string("a,b,,c", ",", "", Parts).       % ["a","b","","c"]
?- split_string("  hi  ", "", " ", [T]).         % trim: T = "hi"
?- string_chars("hi", Cs), string_codes("hi", Ds), string_to_atom("hi", A).
?- number_string(N, " 42 "), number_codes(N2, "17").
?- string_upper("abc", U), string_lower("ABC", L).
?- string_code(1, "abc", C).
?- format(string(S), "~w-~w", [a, b]).           % build a string
?- with_output_to(string(S), (write(a), write(b))).
?- read_term_from_atom('foo(X, bar)', T, []).
?- text_concat(abc, "def", R).                   % mixed text types

18.4 Character classification

Types: alpha, alnum, digit(W), space/white, upper(L), lower(U), punct, csym, csymf, end_of_line, graph.

?- char_type(a, alpha), char_type(X, to_lower(a)), code_type(0'7, digit(W)).
?- char_type(C, upper(l)).     % C = 'L'