Prolog loops and conditional statements?

Is there anything in Prolog that works like a for loop, and if that condition?

+1
source share
2 answers

if / then / else can be obtained with (->) / 2 and (;) / 2:

( If -> Then ; Else ) 

This is sometimes useful. In general, though (when the condition contains variables), this will make your programs unreasonable and incomplete. Whenever you can describe conditions with pattern matching, use pattern matching instead. Then you can not only check, but also generate solutions.

+9
source

If you are looking for these kinds of statements, then you don’t think in Prolog :)

Just joking, by the way, there is no simple translation for or if and otherwise, but you might think about how they should be in the prologue:

  • The if / else statement can only be obtained if there are two rules that correspond to different conditions.
  • a for a loop can be executed with two recursive rules, one of which is basic, and it does not depend on itself, and the other does what you intend to do inside the loop, and it follows itself.
+5
source

Source: https://habr.com/ru/post/888265/


All Articles