user2344504
user2344504

Reputation: 33

Converting 1st letter of atom in Prolog

How to convert 1st letter of an atom in uppercase letter in LPA prolog? the only way I know is using 'lwrupr'. But it capitalizes all the letters.

Upvotes: 1

Views: 1145

Answers (1)

CapelliC
CapelliC

Reputation: 60024

I don't have LPA Prolog available, but here is an educated guess, resulting from a quick read of reference manual: take the first letter from the atom, make it upper case, and rebuild the word

first_char_uppercase(WordLC, WordUC) :-
    atom_chars(WordLC, [FirstChLow|LWordLC]),
    atom_chars(FirstLow, [FirstChLow]),
    lwrupr(FirstLow, FirstUpp),
    atom_chars(FirstUpp, [FirstChUpp]),
    atom_chars(WordUC, [FirstChUpp|LWordLC]).

In SWI-Prolog, we can test it defining the missing builtin lwrupr/2 like this

lwrupr(Low, Upp) :- upcase_atom(Low, Upp).

and we get

?- first_char_uppercase(carlo,X).
X = 'Carlo'.

?- first_char_uppercase('Carlo',X).
X = 'Carlo'.

Upvotes: 2

Related Questions