1

I'm trying to determine which implementation of member is more efficient by drawing the back-chaining procedure of both.

Standard Implementation:

isMember(X,[X|Tail]).  
isMember(X,[H|Tail]) :- isMember(X,Tail).

Append Implementation: (From my class notes)

appendMember(X,List).  
appendMember(X,List) :- myAppend(List1,[X|List2],List).  

myAppend([],List,List).
myAppend([H|List1],List2,[H|Result]) :- myAppend(List1,List2,Result).

When I use the tracer on TK Eclipse I get the expected output for the standard implementation with the recursive calls but the append implementation just exits successfully immediately.

I'm wondering why that is and how to go about drawing the back-chaining procedure for the append method.

Thanks in advance!

Ruhe
  • 79
  • 1
  • 5

1 Answers1

2

appendMember(X,List). is satisfied immediately, no matter what you have for the 2 arguments, since they are unconnected, uninstantiated variables.

As such, I think you should remove it.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Thanks! I was looking at it and thinking how do I even start the back-chaining process? Once I removed it the tracer clearly shows whats happening. – Ruhe Oct 24 '17 at 20:14
  • Sorry but do you know which one would be more efficient for a large list? They both seem to take the same amount of steps for my test case ...Member(p,[d,f,p,r]). – Ruhe Oct 24 '17 at 20:18
  • @Ruhe, SWI prolog has some predicates for [obtaining run-time statistics](http://www.swi-prolog.org/pldoc/man?section=statistics). – lurker Oct 24 '17 at 21:36