Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is the purpose of using cut in \'Prolog\'? What is the difference betwee

ID: 3568674 • Letter: 1

Question

1. What is the purpose of using cut in 'Prolog'? What is the difference between Green and Red cut, explain them using examples.

2. Sort a list of integer numbers in descending order in Prolog language.

Example:

?- des_sort([3, 45, -10, 81, 54, 50], X)

X = [81, 54, 50, 45, 3, -10].

3. Say you are given two sets using two lists in Prolog. Write rules in Prolog to find the union and intersection sets of these two sets.

4. Write Prolog code that computes the number of vowels in a list.

Example:

5 ?- countVowel([a,b,c,d,e,h,i,j,u], Count).

Count = 4.

5. You are given an integer list. Write rules in Prolog to find the median of the list. The median of a finite list of numbers can be found by ordering all the numbers from lowest value to highest value and picking the middle one. As an example, the median of {1, 3, 5, 9, 11} is 5. If there is an even number of observations, the median is then usually defined to be the mean of the two middle value. The median of {1, 3, 7, 9} is (3 + 7) / 2 = 5.

Explanation / Answer

cut in 'Prolog' :

The cut, in Prolog, is a goal, written as !, which always succeeds, but cannot be backtracked past. It is best used to prevent unwanted backtracking, for example, to prevent extra solutions being found by Prolog and avoid additional computations that are not desired or required in a program.

The cut should be used sparingly. There is a temptation to insert cuts experimentally into code that is not working correctly. If a test is unnecessary because a cut has guaranteed that it is true, it is good practice to say so in a comment at the appropriate place.

Green cut and Red cut :

Green cut

A use of a cut which only improves efficiency is referred to as a green cut. For example:

gamble(X) :- gotmoney(X),!.
gamble(X) :- gotcredit(X), + gotmoney(X).

This is called a green cut operator. The ! simply tells the interpreter to stop looking for alternatives. But you'll notice that if gotmoney(X) fails it will check the second rule. Checking for gotmoney(X) in the second rule seems useless since you already know that if Prolog is there then gotmoney(X) failed before, otherwise the second rule wouldn't be evaluated in the first place. However, by explicitly writing + gotmoney(X), you guarantee that the second rule will always work, even if the first one is removed by accident or changed.

Purpose(s): -make program more efficient. -do not change the output of the program.


Red cut

A cut that isn't a green cut is referred as a red cut, for example:

gamble(X) :- gotmoney(X),!.
gamble(X) :- gotcredit(X).

You depend on the proper placement of the cut operator and the order of the rules to determine their logical meaning. If for any reason the first rule is removed (e.g. by a cut-and-paste accident), the second rule will be broken, i.e., it will not guarantee the rule + gotmoney(X).

quick sorting in prolog :

Domains
    list = integer*.

Predicates
    quicksort(list,list).
    split(integer,list,list,list).
    concatenate(list,list,list).
    printlist(list).
  
Clauses
    quicksort([],[]).
    quicksort([Head|Tail],SortedList) :-
        split(Head,Tail,SList,BList),
        quicksort(SList,SList1),
        quicksort(BList,BList1),
        concatenate(SList1,[Head|Blist1],SortedList),
        printlist(SortedList).
  
    split(_,[],[],[]).
    split(Item,[Head1|Tail1],[Head1|SList],BList) :-
        Item > Head1 , ! ,
        split(Item,Tail1,SList,BList).
    split(Item,[Head1|Tail1],SList,[Head1|BList]) :-
        split(Item,Tail1,SList,BList).
  
    concatenate([],List,List).
    concatenate([Item|List1],List2,[Item|List3]) :-
        concatenate(List1,List2,List3).
  
    printlist([]) :- nl.
    printlist([Head|Tail]) :-
        write(Head," "),
        printlist(Tail).

Output

Goal: quicksort([2,4,1,3,5,9,6],L).

1
3
6
6 9
5 6 9
3 4 5 6 9
1 2 3 4 5 6 9
L=[1,2,3,4,5,6,9]
1 Solution

rules in Prolog to find the union and intersection sets of these two sets :

/* e */
union([A|B], C, D) :- member(A,C), !, union(B,C,D).
union([A|B], C, [A|D]) :- union(B,C,D).
union([],Z,Z).

/* f */
intersection([A|B], C, D) :- member(A,C), !, intersection(B,C,D).
intersection([A|B], C, [A|D]) :- intersection(B,C,D).
intersection([],Z,[]).


computes the list of vowels :

cvowel([],0).
vowel([a],Counter):-Counter is 1.
vowel([e],Counter):-Counter is 1.
vowel([i],Counter):-Counter is 1.
vowel([o],Counter):-Counter is 1.
vowel([u],Counter):-Counter is 1.
vowel([_];[],Counter):-Counter is 0.
cvowel([H|T],Counter1):-cvowel(T,Count),vowel(H,Counter),Count is Count
+Counter,Counter1 is Counter1+Count.