如何编写谓词listtran(L,R)?

时间:2012-10-03 12:15:31

标签: prolog

如何编写谓词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].

2 个答案:

答案 0 :(得分: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).

答案 1 :(得分: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.
相关问题