Prolog - 将字符串与整数分开
Prolog - separate string from integer
我有一个类似于此 ['hello world'-2, 'another string'-2,...]
的列表,我需要将字符串与整数分开以便处理两者。关于如何做到这一点的任何想法?我试过这个:
1 ?- term_string('hello world'-2,String),split_string(String,'-',' ',List).
String = "'hello world'-2",
List = ["'hello world'", "2"].
但这给了我一个字符串的字符串,我只想要字符串。
我认为将术语转换为字符串表示形式然后拆分字符串是错误的方法。这有点蛮力和间接。可以直接拆分条款:
split_term(A-B, [A, B]).
split_list(L, R) :- maplist(split_term, L, R).
?- split_list(['hello world'-2, 'another string'-2], R).
R = [['hello world', 2], ['another string', 2]].
我有一个类似于此 ['hello world'-2, 'another string'-2,...]
的列表,我需要将字符串与整数分开以便处理两者。关于如何做到这一点的任何想法?我试过这个:
1 ?- term_string('hello world'-2,String),split_string(String,'-',' ',List).
String = "'hello world'-2",
List = ["'hello world'", "2"].
但这给了我一个字符串的字符串,我只想要字符串。
我认为将术语转换为字符串表示形式然后拆分字符串是错误的方法。这有点蛮力和间接。可以直接拆分条款:
split_term(A-B, [A, B]).
split_list(L, R) :- maplist(split_term, L, R).
?- split_list(['hello world'-2, 'another string'-2], R).
R = [['hello world', 2], ['another string', 2]].