在prolog中从atom拆分整数
我想从原子中拆分一个整数。你知道我该怎么做吗 示例查询:在prolog中从atom拆分整数,prolog,Prolog,我想从原子中拆分一个整数。你知道我该怎么做吗 示例查询: ?- split_int('nc(4)', N). % given: the atom 'nc(4)' N = 4. % expected: the integer 4 在SWI序言中,您应该能够说 ?- term_to_atom( nc(N) , 'nc(4)' ). N = 4. 得到你想要的。在Sicstus中,看起来您需要使用。你可以这样说: atom_to_t
?- split_int('nc(4)', N). % given: the atom 'nc(4)'
N = 4. % expected: the integer 4
在SWI序言中,您应该能够说
?- term_to_atom( nc(N) , 'nc(4)' ).
N = 4.
得到你想要的。在Sicstus中,看起来您需要使用。你可以这样说:
atom_to_term( A , T ) :-
atom_codes( A , Cs ) ,
read_from_codes( Cs , T )
.
尽管你必须确保你的原子被一个句号/句号终止<代码>'nc(4)不起作用,但
'nc(4)
起作用。使用SWI prolog 6编写了以下程序
atom_chars => convert to char list,
and process the char list to get the number characters
and use atom_chars/2, atom_number/2 to change it back to number
?-拆分(nc(4),N)。
N=4
?-拆分整型(“nc(1234)”,N)。
N=1234。
?-atom-to-term('nc(4)'nc(N),[])。
产生:N=4。@Giththan没有像nc(4)
这样的引号,你可以使用split_-int(事实,N):-arg(1,事实,N)。
@matatom-to-to-term/3
它在SICStus prolog中可用吗?如果没有其他替代方法,请查看:基于SICStus的@mat thx,我在中找到的是双引号,而不是像'foo'
-->foo这样的''