序言中永无止境的递归函数问题

问题描述 投票:0回答:1

我正在尝试编写以下函数:

create_APIds( PId, ASA, APIds ) 
ASA: List of type [a01-1, a02-2, a03-1]
PId: an int like 1 for example
APIds: returning list of all the a0s of ASA, which are linked to PId

例如调用

create_APIds(1, [a01-1, a02-1, a03-2], APIds).
时应该返回
APIds = [a01, a02]
.

我尝试了以下方法,但我的实施并没有停止,我不明白为什么:

create_APIds( PId, [], _ ).
create_APIds( PId,[AId-Pid|ASA], APIds ) :-
    Pid is PId, 
    append( APIds, [AId], APIds2   ),
    create_APIds( PId, ASA, APIds2 ).
list recursion prolog
1个回答
0
投票
create_APIds( _, [], [] ).
create_APIds( PId, [AId-PId|ASA], [AId|Rest] ) :-
    create_APIds( PId, ASA, Rest ).
create_APIds( PId, [AId-PId2|ASA], APIds ) :-
    not(PId = PId2),
    create_APIds( PId, ASA, APIds ).
© www.soinside.com 2019 - 2024. All rights reserved.