8

I have a list of Character Codes in prolog.

I would like to change them into characters.

For instance,

L = "abc" returns L = [97,98,99]

Assuming I start with L = [97,98,99]

Is there anyway to convert L back into abc such that, if there exists a method

convert(L, X) returns X = abc

Thanks.

false
  • 10,264
  • 13
  • 101
  • 209
ali
  • 846
  • 2
  • 18
  • 34

3 Answers3

5

Given L="abc", convert(L, X), X = abc I'd say that you want to get atom (see Data types description) from prolog string. I guess you want atom_codes/2 or something like that. It should work like L="abc", atom_codes(X, L). according to doc.

Unnfortunately currently I have no SWI-Prolog in my system. But here is YAP which contains atom_codes/2

YAP 6.3.2 (x86_64-linux): Sat Sep  1 08:24:15 EEST 2012
MYDDAS version MYDDAS-0.9.1
?- L="abc", atom_codes(X,L).
L = [97,98,99],
X = abc

Don't forget as well that if you need to output string you don't need it to be converted to atom. See format/2 in SWI (or in YAP)

?- L="abc", format("~s~n", [L]).
abc
L = [97,98,99]
ony
  • 12,457
  • 1
  • 33
  • 41
4

Use char_code(?Atom, ?ASCII) in a map list.

char_code(?Atom, ?ASCII) Convert between character and ASCII value for a single character. (16)

Source

Isaac
  • 2,701
  • 4
  • 30
  • 47
4

Characters are represented as atoms of length 1. You can produce them:

  • At read time by changing the Prolog flag double_quotes. For more, see this and that link.

    :- set_prolog_flag(double_quotes, chars).
    
  • Dynamically, you can use atom_codes/2 and atom_chars/2. Or you can use char_code/2 directly.

Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209
  • I thought that atoms is kinda abstract identifiers that have no properties except of that they are different if identifiers are different. Conversion to string and back is more like lookup association rather than disassembling and assembling. As well some Prolog systems (at least YAP and one mentioned in question) may represent strings as list of character codes (numbers). Though I agree that numbers might be considered as a subset of atoms (contrary type to list/struct) but their identifiers for visible characters usually is longer than one character. – ony May 03 '16 at 05:33
  • @ony: Also in YAP you can state: `set_prolog_flag(double_quotes, chars)` and then `"abc"` is `[a,b,c]`. In general, this representation is much more readable than `[97, 98, 99]`. – false May 03 '16 at 09:53
  • under "may" I mean they may represent. BTW, I'm not sure that such representation is more compatible. YAP 6.3.3 simply fails to understand text represented in that way. Try `L="abc", format("~s~n", [L]).` and you'll see that it complains with error `TYPE ERROR- format/3: expected text, got [~,s,~,n]`. So I feel that there is a reason why developers of some Prolog systems made chars to be represented by numbers as a default behaviour. – ony May 03 '16 at 21:33
  • Try 6.3.4. Apart from that, `format('~s~n',[L])` is preferable. – false May 04 '16 at 03:27