-1

I'm having trouble solving this prolog problem. Define a postfix predicate so that postfix(X,Y) says that X is a list that is a postfix of Y. That is, each element of X is equal to (unifies with) the corresponding element of Y, but Y may contain additional elements before that.

IN:

postfix(X,[1,2,3]).

OUT:

[]; [3]; [2,3]; [1,2,3];

I tried

postFix(X,[]). 
postFix(X,Y) :- append(,X,Y), [H|T] is Y, postfix(,X,T).

Thanks!

repeat
  • 18,496
  • 4
  • 54
  • 166
Musa Jamal
  • 77
  • 1
  • 6
  • What dit you try yourself so far? Add some examples input/ouput. – Sam Segers Dec 17 '15 at 00:47
  • I have tried using append function like append(_,X,Y). So the user can pass postfix(X,[1,2,3]) then they should get []; [3]; [2,3]; [1,2,3];. – Musa Jamal Dec 17 '15 at 00:48
  • I have tried postFix(X,[]). postFix(X,Y) :- append(_,X,Y), [H|T] is Y, postfix(_,X,T). – Musa Jamal Dec 17 '15 at 00:51
  • Very good, where did you get? What's not working. Show us what you got and where you got stuck in your question. – Sam Segers Dec 17 '15 at 00:51
  • I do not know how exactly to tackle this problem. But I did commented what i did so far. I have Final Exam tomorrow, and i need to learn this. So it will really helpful if someone can explain this step by step using Append function Please. – Musa Jamal Dec 17 '15 at 00:58

1 Answers1

2

You were actually pretty close. It's a lot simpeler So postfix means appending anything before X, that is resulting in Y

This is what append/3 does: append(prefix,postfix,list)

postfix(X,Y) :- append(_,X,Y).

Example:

- postfix(X,[1,2,3]).
X = [1, 2, 3] ;
X = [2, 3] ;
X = [3] ;
X = [] ;
false.

If you want this as a list

postfixList(X,Y) :- findall(P,append(_,P,Y),X);
Sam Segers
  • 1,951
  • 2
  • 22
  • 28