Prolog - List of CharCodes to a String or Characters

风流意气都作罢 提交于 2019-12-19 17:45:39

问题


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.


回答1:


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]



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/13670798/prolog-list-of-charcodes-to-a-string-or-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!