Counter-intuitive behavior of min_member/2

后端 未结 6 1309
一个人的身影
一个人的身影 2021-01-04 16:40

min_member(-Min, +List)

True when Min is the smallest member in the standard order of terms. Fails if List is empty.

?- min_member(3, [1,2,X]).
X = 3         


        
6条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 16:57

    I have an observation regarding your xmin_member implementation. It fails on this query:

    ?- xmin_member(1, [X, 2, 3]).
    false.
    

    I tried to include the case when the list might include free variables. So, I came up with this:

    ymin_member(Min, Lst):-
        member(Min, Lst),
        maplist(@=<(Min), Lst).
    

    Of course it's worse in terms of efficiency, but it works on that case:

    ?- ymin_member(1, [X, 2, 3]).
    X = 1 ;
    false.
    
    ?- ymin_member(X, [X, 2, 3]).
    true ;
    X = 2 ;
    false.
    

提交回复
热议问题