问题
So as the title says - how do you convert a string into an integer? the idea is something like this:
convert(String,Integer).
examples:
convert('1',1).
convert('33',33).
I'm using swi prolog
回答1:
Use atom_number/2. E.g:
atom_number('123', X).
X = 123.
回答2:
Assuming you really meant a string and not an atom, use number_codes.
?- number_codes(11, "11").
true.
?- number_codes(11, Str).
Str = [49, 49]. % ASCII/UTF-8
?- number_codes(N, "11").
N = 11.
回答3:
Perhaps use of atom_codes(?Atom, ?String) and number_chars(?Number, ?CharList) would do it.
回答4:
in Visual Prolog convert:
X=toTerm(real,H).
real/integer/unsigned...
来源:https://stackoverflow.com/questions/6782007/prolog-how-to-convert-string-to-integer