Reputation: 115
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,
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
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
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