-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCUT.pl
38 lines (29 loc) · 1.43 KB
/
CUT.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
% In Prolog, the cut (!) is a control construct used to commit to the choices made during the execution of a program.
% It prunes the search space and prevents backtracking to choices made before the cut.
% The cut is a powerful tool, but it should be used with caution, as it can make the code less readable and harder to understand.
% Here's a simple example to illustrate the use of the cut:
/* Define a predicate to classify a number as positive, negative, or zero */
classify_number(X, positive) :- X > 0, !.
classify_number(X, zero) :- X =:= 0, !.
classify_number(X, negative) :- X < 0.
/* Example usage */
?- classify_number(5, Class).
% Output: Class = positive
?- classify_number(0, Class).
% Output: Class = zero
?- classify_number(-3, Class).
% Output: Class = negative
% End
% The cut (!) and the fail/0 predicate are often used together to control backtracking and terminate search branches.
% The fail/0 predicate awlays fails, and when combined with the cut, it can be used to prune certain paths.
% Here's an example to illustrate the use of cut and fail:
/* Define a predicate to find the first positive element in a list */
first_positive([X|_], X) :- X > 0, !.
first_positive([_|Tail], Result) :- first_positive(Tail, Result).
first_positive([], no_positive).
/* Example usage */
?- first_positive([1, -2, 3, -4, 5], Result).
% Output: Result = 1
?- first_positive([-1, -2, -3], Result).
% Output: Result = no_positive
% End