0

I need to get the first element of pairs however I don't know the type and it's giving me _20668 instead of C. I know it's because it's not a String but how do I convert it into a String to get a C (I can't change the input).

test([]).
test([Index-X|T]) :-
    test(T),
    get_pairs_value(X),
    write('Index : '), write(Index), nl.

?- test([C-['Al'-2,'O'-3]]).

enter image description here

I tried atom_string() but it gives me _20668.

false
  • 10,264
  • 13
  • 101
  • 209
cece
  • 3
  • 3
  • 1
    You can't; `C` in capitals is a variable in Prolog. If you want it to be an uppercase c as text, use `'C'` in single quotes. If you want it to fill in a number as the index, you'll have to write code to do that somehow, like counting or using `nth0`. – TessellatingHeckler Jan 14 '23 at 23:05
  • If you want to preserve variable names you need to [save them while reading](https://stackoverflow.com/a/7948525/772868). – false Jan 17 '23 at 09:48

0 Answers0