Prolog, find minimum in a list

前端 未结 13 1955
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 01:25

in short: How to find min value in a list? (thanks for the advise kaarel)

long story:

I have created a weighted graph in amzi prolog and given 2 nodes, I am

13条回答
  •  感动是毒
    2020-12-07 02:02

    SWI-Prolog has min_list/2:

    min_list(+List, -Min)
        True if Min is the smallest number in List.
    

    Its definition is in library/lists.pl

    min_list([H|T], Min) :-
        min_list(T, H, Min).
    
    min_list([], Min, Min).
    min_list([H|T], Min0, Min) :-
        Min1 is min(H, Min0),
        min_list(T, Min1, Min).
    

提交回复
热议问题