开发者

forall - Prolog

Can someone explain how should the predefine predicate 开发者_StackOverflow中文版forall to find the minimum values within a list?


For a list L, you can use:

member(Min,L), forall(member(N,L), N>=Min).

However, while this is a nice demonstration of forall, it is not efficient (square complexity instead of linear).


or you can use the predicate findall/3

findall(Value, minimumValues(Value), minimumValuesList)

it returns a list (minimumValuesList) with elements (all the minimum values, right).


Why do you want to use forall/2 to find the minimum?!

For the standard (linear) solution for picking the minimum/maximum/... element from a list look at SWI-Prolog's min_list/2:

?- listing(min_list).
lists:min_list([], A, A).
lists:min_list([A|C], B, E) :-
        D is min(A, B),
        min_list(C, D, E).

lists:min_list([B|A], C) :-
        min_list(A, B, C).


Just use this:

minlist([X], X).
minlist([H|T], Min) :-
    minlist(T, Tmin),
    Min is min(H, Tmin), !.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜