Prolog, find minimum in a list?

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 greater than K min_in_list(K|T,M). % so use K.

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 greater than K min_in_list(K|T,M).

% so use K.

Thanks andersoj. I actually saw this code while searching online prior to submitting my qn here. Somehow, I did not understand it, now I do, thanks!

– Roy Oct 19 '10 at 14:50 It's been years since I've thought in Prolog, but I suspect @mat has the readability edge on what I posted... – andersoj Oct 19 '10 at 15:04 The problem with your solution is also that you compare H and K twice, the min-function probably does not do that... – Kaarel Oct 20 '10 at 15:05.

It is common to use a so-called "lagged argument" to benefit from first-argument indexing: list_min(L|Ls, Min) :- list_min(Ls, L, Min). List_min(, Min, Min). List_min(L|Ls, Min0, Min) :- Min1 is min(L, Min0), list_min(Ls, Min1, Min).

Thanks for the replies. Been useful. I also experimented furthur and developed this answer: min_list(X,X).

% if list has only 1 element, it is the smallest. Also, this is base case. Min_list(H|List,X) :- min_list(List,X1), (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!

%Usage: minl(List, Minimum). Minl(Only, Only). Minl(Head|Tail, Minimum) :- minl(Tail, TailMin), Minimum is min(Head, TailMin).

The second rule does the recursion, in english "get the smallest value in the tail, and set Minimum to the smaller of that and the head". The first rule is the base case, "the minimum value of a list of one, is the only value in the list". Test: |?

- minl(2,4,1,1). True? Yes |?

- minl(2,4,1,X). X = 1? Yes You can use it to check a value in the first case, or you can have prolog compute the value in the second case.

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).

I have created a weighted graph in amzi prolog and given 2 nodes, I am able to retrieve a list of paths. However, I need to find the minimum value in this path but am unable to traverse the list to do this. May I please seek your advise on how to determine the minimum value in the list?

Thus, ' keying findall(Z,path(2,6,Z),L).' in listener allows me to attain a list 3,2,2,1. I need to retrieve the minimum value from here and multiply it with an amount. Can someone please advise on how to retrieve the minimum value?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions