lnotik
lnotik

Reputation: 115

Prolog for beginners about logic and syntax

I have this question :

I need to create a paradict "rightGuesses" which will get 3 arguments, each one of them is a list of letters,

  1. The list of guessed letters
  2. The word i have to guess
  3. The letters that where guessed so far.

For example :

rightGuesses([n,o,p,q], [p,r,o,l,o,g], Ans).

will give us

Ans = [p, -, o, -, o, -].

I made:

rightGuesses([],T2,[ANS])  
rightGuesses([A|T1],T2,[ANS]):-
   (member(A,T2))=\=true,
   rightGuesses(T1,T2,[ _ |'-']).
rightGuesses([A|T1],T2,[ANS]):-
   member(A,T2),
   rightGuesses(T1,T2,[ _ |A]).

But I get :

ERROR: c:/users/leonid/desktop/file3.pl:5:0: Syntax error: Operator expected Warning: c:/users/leonid/desktop/file3.pl:6:

When I try to compile it what is my problem, and is there is a better way to do it ?

Upvotes: 3

Views: 123

Answers (2)

false
false

Reputation: 10142

Apart from the errors observed by @CapelliC, there are many more errors. Like the [_|'-'] and much more.

The first observation is that the second and third argument are lists of same length. And in fact, each element corresponds to the element at the same position in the other list. So we have a relation for each letter: guesses_letter_sofar/3:

guesses_letter_sofar(Xs, C, -) :-
   maplist(dif(C), Xs).
guesses_letter_sofar(Xs, C, C) :-
   member(C, Xs).

This can be more compactly expressed using all//1 as:

guesses_letter_sofar(Xs, C, S) :-
   phrase(( all(dif(C)), ({S=(-)}|[C],{S=C}) ).

Now given that, your relation is:

rightGuesses(Guesses, Word, Sofars) :-
   maplist(guesses_letter_sofar(Guesses), Word, Sofars).

Or, more verbosely:

rightGuesses(_Guesses, [], []).
rightGuesses(Guesses, [C|Cs], [Sofar|Sofars]) :-
   guesses_letter_sofar(Guesses, C, Sofar),
   rightGuesses(Guesses, Cs, Sofars).

Upvotes: 1

CapelliC
CapelliC

Reputation: 60034

after rightGuesses([],T2,[ANS]) you miss a dot. I can't spot other syntax errors, but you have a 'semantic' one: this doesnt' make sense: (member(A,T2))=\=true, use \+member(A,T2) instead.

Upvotes: 2

Related Questions