Prolog - List of CharCodes to a String or Characters

后端 未结 3 914
难免孤独
难免孤独 2021-01-18 03:22

I have a list of Character Codes in prolog.

I would like to change them into characters.

For instance,

L = \"abc\" returns L = [9

3条回答
  •  旧巷少年郎
    2021-01-18 04:17

    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]
    

提交回复
热议问题