Commits

Edd Barrett committed 5f84d07

Re-implement min_member/2 and max_member/2. Remove Var ordering tests (see comments in code)

Comments (0)

Files changed (2)

prolog/prolog_modules/list.pl

 subtract([H|T], D, [H|R]) :-                                                    
 	subtract(T, D, R).
 
-min_member(X, [X]).
-min_member(X, [H|T]) :-
-        min_member(X, T),
-        X @< H.
-min_member(H, [H|T]) :-
-        min_member(X, T),
-        X @>= H.
+% min_member/2
+min_member(Result, [H | T]) :-
+	min_member(H, T, Result).
 
-max_member(X, [X]).
-max_member(X, [H|T]) :-
-        max_member(X, T),
-        X @> H.
-max_member(H, [H|T]) :-
-        max_member(X, T),
-        X @=< H.
+min_member(Result, [], Result).
+min_member(BestIn, [H | T], BestOut) :-
+	(BestIn @< H -> BestDown = BestIn; BestDown = H),
+	min_member(BestDown, T, BestOut).
+
+% max_member/2
+max_member(Result, [H | T]) :-
+	max_member(H, T, Result).
+
+max_member(Result, [], Result).
+max_member(BestIn, [H | T], BestOut) :-
+	(BestIn @> H -> BestDown = BestIn; BestDown = H),
+	max_member(BestDown, T, BestOut).

prolog/prolog_modules/test/test_list.py

     assert_true("subtract([a, b, c], [], [a, b, c]).", e)
     assert_true("subtract([1, 1, 6], [1, 6], []).", e)
 
-def test_min_member_var():
-    assert_true("min_member(X, [TEA,UNIVERSE, SHIRT]), " + \
-            "X @=< TEA, X @=< UNIVERSE, X @=< SHIRT.", e)
+# This is really hard to test as variable addresses can change as their
+# binding changes. This is really a problem with @<. See the test for
+# this for a counter-example (../interpreter/test/test_standard_order.py).
+#
+#def test_min_member_var():
+#    assert_true("min_member(X, [TEA, UNIVERSE, SHIRT]), " + \
+#            "X @=< TEA, X @=< UNIVERSE, X @=< SHIRT.", e)
 
 def test_min_member_number():
     assert_true("min_member(444, [444,445,999]).", e)
 def test_min_member_empty():
     assert_false('min_member(X, []).', e)
 
-def test_max_member_var():
-    assert_true("max_member(X, [TEA,UNIVERSE, SHIRT]), " + \
-            "X @>= TEA, X @>= UNIVERSE, X @>= SHIRT.", e)
+# Same issue as test_min_member_var. See above.
+#def test_max_member_var():
+#    assert_true("max_member(X, [TEA,UNIVERSE, SHIRT]), " + \
+#            "X @>= TEA, X @>= UNIVERSE, X @>= SHIRT.", e)
 
 def test_max_member_number():
     assert_true("max_member(999, [444,445,999]).", e)