0

I realize this is very basic, but I could not work this out from Prolog tutorials, so I hope someone here could help me solve my problem.

I have a term which is true if one of several conditions applies:

answer -->
  Var,
  a([Att1],[Var1]),
  a([Att2],[Var2]),
  a([Att3],[Var3]),
  {
    [one,   two,   three] = [Att1, Att2, Att3] -> Var1 = Var; % first condition
    [one,   three, two]   = [Att1, Att2, Att3] -> Var1 = Var; % second condition
    [two,   one,   three] = [Att1, Att2, Att3] -> Var2 = Var; % third condition
    [three, one,   two]   = [Att1, Att2, Att3] -> Var2 = Var; % fourth condition
  }

All Attributes and Variables come with a fixed value, and I want to offer "answer", if any of the conditions in the "{}" section are fulfilled - but for some reason, it doesn't work. The thing is, if I just check for one of the conditions, say, the first, it works as expected. But I don't want to copy/paste the rule 4 times because I didn't get a logical "or" to work properly.

Just to put it into words, in case I coded something completely different, the first condition is meant to say: check if Att1 is equal to one and Att2 is equal to two and Att3 is equal to three. If that is the case, also confirm that Var1 is equal to the value in Var. If not, check if any of the other conditions can be resolved.

edit: Turns out I just had a ';' too much in the code.

false
  • 10,264
  • 13
  • 101
  • 209
Arne
  • 17,706
  • 5
  • 83
  • 99
  • this doesn't work ? `[one, two, three, Var1] = [Att1, Att2, Att3, Var] -> true ; ... ` – CapelliC Aug 22 '14 at 10:10
  • right, I can write it like that. Do I need to put the -> true there, or can I just link the conditions with ; ? @CapelliC edit: I tested it with that line instead, it's still the case that if I only use one condition it words fine, but if I use all for none works. – Arne Aug 22 '14 at 10:19
  • 1
    it depends... from your description, seems that `;` should do. Do you want it to backtrack when your grammar has other alternative answers ? – CapelliC Aug 22 '14 at 10:21
  • 1
    @CapelliC I am not sure if backtracking is the right word. If the first conditions fails at some point, I want it to try out the next. I could just make 4 times the same rule with just one of the conditions each. – Arne Aug 22 '14 at 10:27
  • @CapelliC it works now, thank you for your help. I just noticed that the 4th condition also ends in a ';' .. dumb error – Arne Aug 22 '14 at 10:29

0 Answers0