Question: Prolog ( 3 0 Points ) ( NOTE: Below we are using Prolog notation. Variables are Uppercase. Constants are lowercase. ) In class I showed
Prolog Points
NOTE: Below we are using Prolog notation. Variables are Uppercase. Constants are lowercase.
In class I showed how sortedlists can be merged in a sorted fashion. That means we are so close to implementing mergesort! We will implement mergesort below.
Example output:
mergeSortL
L
mergeSortL
L
mergeSortL
L
mergeSortL
L
Our program will be implemented with predicates:
mergeSort: which in comes an unsorted list, out goes the sorted list.
merge: which merges the
splitways:
insertIn and insertIn:
So let us go
Finish mergeSort:
mergeSort : Base case when list is empty
mergeSortXX : Base case when list has only item
YOUR CODE TO HANDLE GENERAL CASE
Split the in coming list into sublists
Recursively sort both lists
Merge both lists into the output list
Here is all the code for merge
This is all of it there is nothing else to add.
mergeLL
mergeLL
mergeFRFRFL :
F F
mergeRFRL
mergeFRFRFL :
F F
mergeFRRL
Here is all the code for splitways
This is all of it there is nothing else to add.
splitwaysOriginalRR :
insertInOriginalRR
You must finish the predicates
insertIninsertIn
insertIninsertIn
Both have arguments:
The incoming list
lists which hold temporary lists that build the answers
lists which will hold the answers
Both predicates will have the same base case facts corresponding to when the list to split is empty. It is:
insertInRRRR
It means "When there are no items to split, the temporary lists R and R are also the answer lists."
Your job is to write the rules that build the temporary lists:
YOUR insertIn RULE HERE
insertInRRRR This is the base case
YOUR insertIn RULE HERE
insertInRRRR This is the base case
What are the remaining rules?
Hints:
Both should separate the list to split into a head and a tail HT
Both should build the a list a their respective position
insertIn should call insertIn
insertIn should call insertIn
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
