Se the prolog language for sorting:
a. Write prolog clauses that define the predicate sorted(l), which is true if and only if list l is sorted in ascending order.
b. Write a prolog definition for the predicate perm(l,m), which is true if and only if l is a permutation of m.
c. Define sort(l,m) (m is a sorted version of l) using perm and sorted.
d. Write a faster sorting algorithm, such as insertion sort or quicksort, in prolog.
Answers
This exercise looks at sorting in Prolog.
a. Write Prolog clauses that define the predicate sorted(L), which is true if and only if list L is sorted in ascending order.
b. Write a Prolog definition for the predicate perm(L,M), which is true if and only if L is a permutation of M.
c. Define sort(L,M) (M is a sorted version of L) using perm and sorted.
d. Runsort on longer and longer lists until you lose patience. What is the time complexity of your program?
e. Write a faster sorting algorithm, such as insertion sort or quicksort, in Prolog.
Step-by-step solution:
Step 1 of 3
Prolog is a logical programming language, which is laid using the basics from First-order logic. Prolog parses the natural language and its clauses are generally used as Context-free grammar rules.
A Prolog clause that defines the sorted(L) predicate is as follows:
sorted([])
sorted([A])
sorted([A,B|L]) :- A < B, sorted([B|L]).
The clause initially declares a sorted() function with an empty list. The list A is the first element, which is passed to the function. The element B represents the next first element of the list and L represents elements until the end of the list. The clause is true only when A < B. The list L is sorted in ascending order.
@H¥DRA™