Prolog: how to convert string to integer?

微笑、不失礼 提交于 2019-12-23 10:40:05

问题


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

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