Steve Lou
Steve Lou

Reputation: 13

Prolog won't return true(yes) when it should

mergelist([],[],[]).
mergelist([X],[],[X]).
mergelist([],[Y],[Y]).

I'm running this query ?-mergelist([1],[],[1]). which returns true

But then I run this query ?-mergelist([1,2],[],[1,2]).it returns false(no).

I'm not sure what's wrong.

I'm using ECLiPSe 6.1

Upvotes: 0

Views: 95

Answers (1)

R. Tanguy
R. Tanguy

Reputation: 56

Prolog can't pattern match [1,2] with any of:

  • []
  • [X]

[X] is specifically designed to pattern match with a one-element list.

Try this:

mergelist(L,[],L) :- is_list(L).
mergelist([],L,L) :- is_list(L).

Upvotes: 3

Related Questions