Question: I'm trying to write a prolog program which needs to have a predicate that does returns minimum variable every time it called. But i need it in such a way that if you yield the function, it should return next minimum value. For example:

```
min([5,3,2,7],B).
B = 2 ;
B = 3 ;
B = 5 ;
B = 7 ;
false.

```

Any ideas?

Answer: 
```
min(L, Min) :- sort(L, S), member(Min, S).

```

will do for your example data. More generally, [setof](http://www.swi-prolog.org/pldoc/doc_for?object=setof/3)/3 will allow to build a sorted list of solutions to a generic goal.

*edit*: without sorting, a somewhat involved solution based on [select](http://www.swi-prolog.org/pldoc/doc_for?object=select/3)/3:

```
min(L, M) :-
    select(T, L, R),
    ( maplist('<'(T), R), (M = T ; min(R, M)) ).

```

yields

```
?- min([2,6,1],X).
X = 1 ;
X = 2 ;
X = 6 ;
false.

```
In general, you cannot rely on the order of solutions, in a somewhat weird system member might be implemented differently:

```
member2(X,[_|Xs]) :-
    member2(X,Xs).
member2(X,[X|_]).

min(L, Min) :- 
    sort(L, S), 
    member2(Min, S).

```

yielding:

```
?- min([2,1,4,3,2],X).
X = 4 ;
X = 3 ;
X = 2 ;
X = 1.

```

it becomes reliable if the order is made explicit by putting the original list into relation with a list of the solutions in correct order. In your case this amounts to the sort predicate (as long as you accept that double entries are not reported twice), for a general terminating predicate, this can be done by using setof/2 and determining the order in a seperate step, as CapelliC already mentioned.