1

如何写一个谓词listtran(L, R),
L是[0,1,2,3,4,5,6,7,8,9,10],
R是[零,一,...,十]

例子:

?- listtran([0,4,5], L).  
L = [zero, four, five].  
?- listtran(L, [two, ten, two]).  
L = [2, 10, 2].
4

2 回答 2

2

如果你只需要从 0 到 10,我肯定会开始构建一个将数字转换为文本名称的谓词:

num(0,zero).
num(1,one).
num(2,two).
num(3,three).
num(4,four).
num(5,five).
num(6,six).
num(7,seven).
num(8,eight).
num(9,nine).
num(10,ten).

然后在 listtran 谓词中使用它们很容易:

listtran(IntLst,TxtLst) :-
    maplist(num,IntLst,TxtLst).

要在没有辅助 maplist 谓词的情况下以更清晰的方式构建它,请尝试以下操作:

listtran([],[]). %base rule
listtran([Int|IntRest], [Txt|TxtRest]) :-
    num(Int,Txt),
    listtran(IntRest,TxtRest).
于 2012-10-03T13:10:34.530 回答
1

形成一个配对域,PairDom = [0-zero, 1-one, 2-two, ...]并使用member( X1-Y1, PairDom)

pair(A,B,A-B).

listtran(L,R):-
    maplist(pair,[0,1,2,3, ...,10],[zero,one, ...,ten],PairDom),
    maplist(pair,L,R, ...),
    maplist(member, ...).

要了解这可能如何工作,请尝试:

?- PairDom=[0-zero, 1-one, 2-two], member(1-Y1,PairDom).
Y1 = one

?- PairDom=[0-zero, 1-one, 2-two], member(X1-three,PairDom).
No.
于 2012-10-03T12:31:57.317 回答