Template compatibility and protection is one way to do this; OCaml, Haskell, and Scala also provide them.
The prolog has a similar function: you can define relationships that depend on specific values. For instance:
factorial(X, 0) :- X =< 0, !. factorial(1, 1). factorial(X, Y) :- X2 is X - 1, factorial(X2, Z), Y is X * Z.
In this code, we define a factorial relation such that factorial(X,Y) is executed when Y = X !; To do this, we specialize in three cases: one of which contains a certain value, and the other - a range test.
Yes, the prologue is really strange. Programming consists of recording true statements; you then query the system for the truth of a particular statement or assignment to a variable, which makes the statement true. For example, if the above code is saved in factorial.pl :
?- consult(factorial). % factorial compiled 0.00 sec, 2,072 bytes true. ?- factorial(3, 6). true . ?- factorial(5, X). X = 120 . ?- factorial(4, 25). false.
source share