Subject: Re: [SWIPL] hello,problem with next of min

From: "Richard A. O'Keefe" <[email-masked]>
Date: Tue, 11 Dec 2001 14:23:59 +1300 (NZDT)

Sebastian Schneider <[email-masked]> wrote:
	i try the following:
	
	- find the min of two list

What do you mean?  What _is_ "the min of two list"?
What exactly did you _do_?

	- find the next min from two list without the first min

What do you mean?  What _is_ "the next min"?
What exactly did you _do_?  (See below.)

	- save all min
	
I haven't the least idea what this means.  What did you do?

	the first and the last works, but not the second, can anybody
	help me please?
	
Help us to help you.
	
"The" minimum element of a list is not terribly well defined.
What is the comparison?  Is it arithmetic comparison or term comparison
or something else?  If the smallest value occurs more than once, which
do you take (an important question when comparison doesn't examine
an entire term but just part of it)?

I'm assuming

    min([X|Xs], Min) :-
        min_loop(Xs, X, Min).

    min_loop([], Min, Min).
    min_loop([X|Xs], Min0, Min) :-
        ( X @< Min0 -> Min1 = X ; Min1 = Min0 ),
        min_loop(Xs, Min1, Min).

"The next min from a list without the first min"
could mean to find the next smallest element (_however_ many copies
of the first value there are) or it could mean to find the smallest
element (if the first/last/some other specified copy of the value
were removed).

/*  min_except(Xs, Avoid, Min) :-
        append(Before, [Avoid|After], Xs),    
        !,
        append(Before, After, Ys),
        min(Ys, Min).
*/
    min_except([X|Xs], X, Min) :- !,
        min(Xs, Min).
    min_except([X|Xs], Avoid, Min) :-
        min_except_loop(Xs, Avoid, X, Min).

    min_except_loop([], _, Min, Min).
    min_except_loop([X|Ys], X, Min0, Min) :- !,
        min_loop(Xs, Min0, Min).
    min_except_loop([Y|Ys], X, Min0, Min) :-
        ( Y @< Min0 -> Min1 = Y ; Min1 = Min0 ),
        min_except_loop(Ys, X, Min1, Min).

/*  min_above(Xs, Avoid, Min) :-
        findall(Y, (member(Y, Xs), Y @> Avoid), Ys),
        min(Ys, Min).
*/
    min_above([X|Xs], Avoid, Min) :-
        ( X @> Avoid ->
            min_above_loop(Xs, Avoid, X, Min)
        ;
	    min_above(Xs, Avoid, Min)
	).

    min_above_loop([], _, Min, Min).
    min_above_loop([X|Xs], Avoid, Min0, Min) :-
        ( X @=< Avoid ->
            min_above_loop(Xs, Avoid, Min0, Min)
	; X @< Min0 ->
	    min_above_loop(Xs, Avoid, X, Min)
	;
	    min_above_loop(Xs, Avoid, Min0, Min)
	).

How _two_ lists would be involved escapes me.

BEWARE!  This code has not been tested!