Prolog, find minimum in a list

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

    thanks for the replies. been useful. I also experimented furthur and developed this answer:

    % if list has only 1 element, it is the smallest. also, this is base case.
    min_list([X],X).
    
    min_list([H|List],X) :-
    min_list(List,X1), (H =< X1,X is H; H > X1, X is X1).
    
    % recursively call min_list with list and value,
    % if H is less than X1, X1 is H, else it is the same. 
    

    Not sure how to gauge how good of an answer this is algorithmically yet, but it works! would appreciate any feedback nonetheless. thanks!

提交回复
热议问题