Could somebody explain me what does "!" do in Prolog ? I don't understand it. Here I have a code that counts how many sublists of a heterogeneous list have mountain aspect.
nrSubliste([], 0).
nrSubliste([H|T], R):-
is_list(H),
munteMain(H),!,
nrSubliste(T, R1),
R is R1 + 1.
nrSubliste([_|T], R):-
nrSubliste(T, R).
munteMain
verifies if a linear list has a mountain aspect.
Prolog | An Introduction. Introduction : Prolog is a logic programming language. It has important role in artificial intelligence. Unlike many other programming languages, Prolog is intended primarily as a declarative programming language. In prolog, logic is expressed as relations (called as Facts and Rules).
The prolog operator is a function to operate and works on the programming operand or variable. The prolog operator is a symbolic character to work arithmetic, logical, and comparison operations.
However, other languages like Prolog, have declarative and also imperative properties. This may also include procedural statements like “To solve the problem H, perform B1, B2 and B3”. ALF (algebraic logic functional programming language).
The prolog's run time system provides the service of an interface engine. A basic logic programming environment has no literal values. An identifier with upper case letters and other identifiers denote variables. Identifiers that start with lower-case letters denote data values.
Exclamation point !
denotes Cut in Prolog, a special goal that always succeeds, and blocks backtracking for all branches above it that may have alternatives.
In your case it means that once a solution to munteMain/1
has been found, the program will never backtrack and look for an alternative solution. Specifically, Prolog will never consider the third clause of your nrSubliste/2
rule, i.e. the one ignoring list head with _
, if H
in the second clause is such that munteMain(H)
succeeds.
Note that using !
makes your code is somewhat harder to read and maintain, because the logic in the third clause depends on the logic of the second clause. You can rewrite your program without a cut using the not provable operator \+
:
nrSubliste([H|T], R):-
is_list(H),
munteMain(H),
nrSubliste(T, R1),
R is R1 + 1.
nrSubliste([H|T], R):-
is_list(H),
\+ munteMain(H),
nrSubliste(T, R).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With