-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.pl
58 lines (44 loc) · 1.43 KB
/
Strings.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
% Examples of string representation using atoms
string_atom('Hello World').
string_atom('Prolog is fun!').
% Predicate to check if a term is an atom representing a string
is_string_atom(Term) :-
atom(Term),
atom_chars(Term, Chars),
maplist(char_type(_, alpha), Chars).
% Example usage
?- is_string_atom('Hello World').
% true
?- is_string_atom('123').
% false (contains numeric characters)
?- is_string_atom('Prolog').
% true
% Examples of string representation using lists of characters
string_list(['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']).
string_list(['P', 'r', 'o', 'l', 'o', 'g', ' ', 'i', 's', ' ', 'f', 'u', 'n', '!']).
% Predicate to check if a term is a list representing a string
is_string_list(Term) :-
is_list(Term),
maplist(char_type(_, alpha), Term).
% Example usage
?- is_string_list(['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']).
% true
?- is_string_list([49, 50, 51]).
% false (contains numeric character codes)
?- is_string_list(['P', 'r', 'o', 'l', 'o', 'g', ' ', 'i', 's', ' ', 'f', 'u', 'n', '!']).
% true
% Examples of string representation using the built-in string type
string('Hello World').
string('Prolog is fun!').
% Predicate to check if a term is a string
is_string(Term) :-
string(Term),
string_length(Term, Len),
Len > 0.
% Example usage
?- is_string('Hello World').
% true
?- is_string('123').
% false (contains numeric characters)
?- is_string('Prolog').
% true