问题描述
如何使用 maplist 将字符串更改为原子。
这不起作用:
?- maplist(atom_string,["a","b","c"]).
首先是因为 atom_string/2 有两个参数(你如何做部分函数//在序言中柯里化)。
但即使partial-fun有效,复杂的是atom_string的第一个参数是未知的,即调用是:
atom_string(A,"atom")
不是:
atom_string("atom",A)
这有效:
?- use_module(library(lambda)).
?- F = \Y^X^(atom_string(X,Y)),maplist(F,"c"],L).
F = \Y^X^atom_string(X,Y),L = [a,b,c].
解决方法
这按预期工作:
?- maplist(atom_string,Atoms,["a","b","c"]).
Atoms = [a,b,c].
如果这不是您所追求的,请解释。
,使用辅助谓词。
string_atom(String,Atom) :-
atom_string(Atom,String).
然后使用
运行?- maplist(string_atom,"c"],Atoms).
Atoms = [a,c].