Prolog, find minimum in a list

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

    This is ok for me :

    minimumList([X], X).        %(The minimum is the only element in the list)
    
    minimumList([X|Q], M) :-    % We 'cut' our list to have one element, and the rest in Q
     minimumList(Q, M1),         % We call our predicate again with the smallest list Q, the minimum will be in M1
     M is min(M1, X).            % We check if our first element X is smaller than M1 as we unstack our calls
    
    

提交回复
热议问题