Prolog, find minimum in a list

前端 未结 13 1929
佛祖请我去吃肉
佛祖请我去吃肉 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 01:41

    This looks right to me (from here).

    min_in_list([Min],Min).                 % We've found the minimum
    
    min_in_list([H,K|T],M) :-
        H =< K,                             % H is less than or equal to K
        min_in_list([H|T],M).               % so use H
    
    min_in_list([H,K|T],M) :-
        H > K,                              % H is greater than K
        min_in_list([K|T],M).               % so use K
    

提交回复
热议问题