prolog:查询列表中的范围搜索

时间:2013-12-28 13:15:58

标签: prolog

我需要编写在列表中给定范围内搜索的查询

我试试这个,但它不起作用

find(R1, R2, Z, L):-
    D=1,
    element_at(X, Z, D),
    X >= R1,
    X <= R2,
    add(X, L, [X|L]),
    D+1.

它给了我false

但我需要返回包含此范围内整数的列表

提前感谢。

1 个答案:

答案 0 :(得分:0)

如果我理解了这个问题,我认为你可以这样做:
回答已修改

% when the list is empty, there is nothing between R1 and R2
find(_R1, _R2, [], []).

% I test the first element of the list
% then I process the rest of the list
% L1 is unified after the call of find
find(R1, R2, [[Age, Name] | T], L):-
    (   (R1 =< Age, Age =< R2)
    ->  L = [Name | L1]
    ;   L = L1),
    find(R1, R2, T, L1).