[mercury-users] New to mercury: structure reuse and efficiency

Gregory D. Weber gdweber at indiana.edu
Thu Jul 1 08:35:18 AEST 2004


There's an implied question here which I don't think has been answered:
how _does_ he do what he wants to do here, which, I believe, is to
make sure that H does not equal T before making the recursive call?

diffense writes:
 > 
 > 
 > Ralph Becket <rafe at cs.mu.OZ.AU> wrote:
 > 
 > diffense, Tuesday, 22 June 2004:
 > 

(dot dot dot)

 > 
 > >The compiler has great freedom to rearrange your code!
 > 
 > >The upshot of this is that the `not(X = Y)' goal in there (I presume the
 > >`Y' is a typo?) is superfluous and probably doesn't do what I think
 > >you were trying to achieve.
 > 
 > Yes, it was a mistype. It should have been.
 > 
 > member([H|T], H).
 > 
 > member([H|T], X) :- not(X=H), member(T, X).
 > 
 > I have a little experience with prolog and I remember the order of clauses
 > being quite important so I (wrongly) assumed the same of mercury. IIRC the
 > interpreter would also try to prove subgoals in left to right order. Now after

Isn't the solution to write -- ?

member([H | T], X) :-
  (
    H = X ->
    true
  ;
    member(T, X)
  ).

or, equivalently,

member([H | T], X) :-
  (
    if H = X 
    then true
    else member(T, X)
  ).

-- 
Gregory D. Weber

Associate Professor of Computer Science, Indiana University East
2325 Chester Boulevard, Richmond, Indiana 47374-1289, U.S.A.
Telephone: (765) 973-8420         World-Wide Web http://mypage.iu.edu/~gdweber/

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list