Shmn
Shmn

Reputation: 703

How list of prefix works in prolog

I tryed to understand how this code works but i couldn't.

prefix(P,L) :- append(P,_,L).

I used query like that prefix(X,[a,b,c,d]).If you have got a time, can you explain to me how it works?. Thank you.

Upvotes: 2

Views: 262

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

Looking at append you can see that

append(L1,L2,L3) will hold when the list L3 is the result of concatenating the lists L1 and L2 together (concatenating means joining the lists together, end to end)

So what your predicate above says that something is a prefix of something, if you can append anything (that is the _) to the former and obtain the latter.

Makes a lot of sense, doesn't it? Prolog is nice.

Upvotes: 2

Related Questions