1
votes

Am writing a program that includes a definition for the predicate 'word_replacements/2'. This predicate should be true if the two arguments are lists, and the second list is the same as the first but with all elements that are the single letter 'a' replaced by the letter 'e', and with all elements that are the single letter 'e' replaced by the letter 'a'. Your answer should reproduce the following example input/output:

?- word_replacements([e, a, s, i, l, y],Word_replacements).
Word_replacements = [a, e, s, i, l, y];
false.

?- word_replacements(Word, [a,e,s,i,l,y).
Word = [e, a, s, i, l, y];
false.

This is what I have tried but it just gives me false.

word_replacements([],[]). 
word_replacements([H|T], Word_replacements):-
   word_replacements(H,Replace_A),
   word_replacements(T,Replace_E),
   Append(Replaced,[T],Word_replacements).
1
You should get lots of warnings with that definition.false
False, yes i did but since am new to prolog, i got stuck. kindly help me. thaksAnis
please atleast give me pointers or clues to solve this.Anis
You probably saw warnings about Replace_A and Replace_E being singleton variables. It's because you used these variables ones but note elsewhere. So they don't serve any purpose. Also, your definition of word_replacements/2 assumes the first argument is a list, but word_replacements(H, Replace_A) passes a non-list as the first argument. You need to review your logic to resolve these issues.lurker

1 Answers

0
votes

A first thing to understand about Prolog is that you cannot reassign variables because of unification. Once you assign a value to a variable, it will never change again. This has implications for your example, since it seems you are trying to replace variables holding specific characters.

Of course there are various ways to solve this, but since you stated that you are new to prolog, I'll try to provide a (unfinished) simple way of writing this:

Firstly, we'll evaluate your code:

word_replacements([],[]). 
word_replacements([H|T], Word_replacements):-
   (1)  word_replacements(H,Replace_A),
   (2)  word_replacements(T,Replace_E),
   (3)  append(Replaced,[T],Word_replacements).

What you wrote is the following:

(1) : recursive call to word_replacements, however with H as first argument, since H is not a list, but an element, this will never pattern match and thus execution will fail here.

(2) : recursive call to word_replacements, this time with T, the tail of the list, and this indeed is a list as well, so that would be correct. However, as second argument, you specify a new uninstantiated variable 'Replace_E). Prolog does not know where this variable comes from and will thus give you the warning 'Singleton variable'. When you make recursive calls, you want to end up with a result afterwards, this means you will have to pass a known variable between recursive calls, instead of a new singleton variable.

(3) : here you try to append another new variable 'Replaced' with the tail of the letters enclosed in square brackets. This would wrap the tail, which is already in a list, into another list and you would end up with something like [['l','y']], which is not what you want.

Okay, we'll now start by breaking down the problem into the following possible states (this used to help me alot when I was new to Prolog):

  • The specified list of letters is empty
  • The current letter being read is an 'a'
  • The current letter being read is an 'e'
  • The current letter being read is some other letter than 'a' or 'e'

We now try to translate this into prolog code:

% Empty list
word_replacements([],[]).

% If H is an 'e', we want to add an 'a' to our result
word_replacements([H|T],[a|R]) :-
    ... 
    word_replacements(T,R).

% If H is an 'a', we want to add an 'e' to our result
word_replacements([H|T],[e|R]) :-
    ...
    word_replacements(T,R).

% If H is anything other than an 'a' or 'e', we want to keep that letter
word_replacements([H|T],[H|R]) :-
    ...
    word_replacements(T,R).

As you can see, we now wrote a structured model in which it is easy to specify different behaviour for different situations.

All that's left to do now, is for you to specify the conditions for each situation.

Good luck!