5 Replies - 813 Views - Last Post: 15 April 2011 - 01:08 AM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

New to Prolog Program writing

Posted 09 April 2011 - 05:20 PM

I am suppose to be writing a program that creates a prefix and postfix definition and stores it in a file to be called from SWI-Prolog application. I created a file called prepost.pl and am trying to figure out exactly how to make it work... here are the examples he gave us:
?- consult(prepost).
% prepost compiled 0.00 sec, 956 bytes
true.

?- prefix([a,b,c],[a,b,c,e,f]).
true.

?- prefix([a,b,c], [a,b,e,f]).
false.

?- prefix([a,b],[a]).
false.

?- prefix([],[a,b,c,d]).
true.

?- prefix(X,[a,b,c,d]).
X = [] ;
X = [a] ;
X = [a, b] ;
X = [a, b, c] ;
X = [a, b, c, d] ;
false.

?- postfix([n,e],[d,o,n,e]).
true .

?- postfix([],[a,n,y,t,h,i,n,g]).
true .

?- postfix([a,b,c],[a,b,c,d,e]).
false.

?- postfix(X,[a,b,c,d]).
X = [a, b, c, d] ;
X = [b, c, d] ;
X = [c, d] ;
X = [d] ;
X = [] ;
false.



my first idea was to use this :


  sublist(_List1, _List2).




but when I tested it, it didn't really work... then I thought about just using the word prefix


   prefix(_List1, _List2).




I am just not sure where to go with this. My teacher said to use lists but not sure what i should be doing. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: New to Prolog Program writing

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: New to Prolog Program writing

Posted 09 April 2011 - 09:00 PM

This is a trivial problem, you just need to learn how to think declaratively. First lets figure out the base case which is the easy part, we all know that an empty list is a prefix of any other list, so we got ourselves this predicate
prefix([], _).



Now lets try to figure out the recursive case. First of all how do we know if a list is a prefix of another list? simple, if the first list occurs in the beginning of the other list, in other words if the 1st element of list1 is equal to the 1st element of list2, 2nd element of list1 is equal to the 2nd element of list2, and so on and so forth. Lets now put what we understood in a clear sentence. We can say that a list is a prefix of another list if the 1st element of the 1st list is equal to the 1st element of the 2nd list, and if the rest of the 1st list is a prefix of the rest of the 2nd list. Now we have ourselves a clear and recursive definition that directly maps to a prolog rule.
prefix([], _).
prefix([H1|T1], [H1|T2]):-
    prefix(T1, T2).



I hope you now got the idea of how to approach a problem in prolog.
Was This Post Helpful? 0
  • +
  • -

#3 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: New to Prolog Program writing

Posted 10 April 2011 - 10:42 AM

so to do the postfix would you do this:

postfix(_, []).
postfix([T1|H1], [T1|H2]) :-postfix(H1, H2).


Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: New to Prolog Program writing

Posted 10 April 2011 - 03:29 PM

that doesn't look correct. I will leave you to find the solution on your own, just abstract away any details and think declaratively and it shouldn't be tough to solve the problem.
Was This Post Helpful? 0
  • +
  • -

#5 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: New to Prolog Program writing

Posted 11 April 2011 - 08:22 AM

I was thinking that the way to do it is to start at the end of the list and trace backwards to see if the elements match but don't know how to start at the end of the list. Asked my teacher for some advice and he said to use append but not sure exactly what I am appending together or why I would be appending?? I don't want the solution, just a tip to head in the right direction...
with append I tried this but it doesn't make sense...

postfix(_, []).
postfix(append[T1], X, [H1]) :-postfix(T1, H1).



Why would you want to append anything in this case??
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: New to Prolog Program writing

Posted 15 April 2011 - 01:08 AM

the word append alone should be a hint enough for you :), maybe this will clear it up

list A
[1, 2, 3]
list B
[a, b, c, 1, 2, 3]

as you see list A is a postfix of list B, now what is the relationship between list A and list B, and how can you use the append predicate to make list B from list A. I think you got it now :)

This post has been edited by mostyfriedman: 15 April 2011 - 01:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1