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

Define the following Prolog predicates without using library functions (except w

ID: 3598670 • Letter: D

Question

Define the following Prolog predicates without using library functions (except where noted):

(a) increasing(List) which is true if List is a monotonically increasing list of numbers

(b) decreasing(List) which is true if List is a monotonically decreasing list of numbers

(c) allsame(List) which is true if all elements of a List are the same

(d) notsame(List) which is true if all elements of a List are not the same

(e) alldifferent(List) which is true if all elements of a list are different (same as making is_set)

(f) sorted(List) which is true if List is sorted (increasingly or decreasingly)

(g) analyze(List) which displays the message ‘This list is sorted’, or ‘This list is not sorted’ depending on the values in the list

(h) minplusmax(List, MinPlusMax) which returns the sum of the minimum and the maximum element of the List

Notes:

(1) Performance of these predicates is not critical (all reasonable solutions are OK);

(2) Monotonically increasing means =<;

(3) In (e) you may use the library function member;

(4) In (h) you may use all library functions;

(5) In definitions of new predicates, you may use your previously defined predicates;

Define the following Prolog predicates without using library functions (except where noted): (a) increasing(List) which is true if List is a monotonically increasing list of numbers (b) decreasing(List) which is true if List is a monotonically decreasing list of numbers (c) allsame(List) which is true if all elements of a List are the same (d) notsame(List) which is true if all elements of a List are not the same (e) alldifferent(List) which is true if all elements of a list are different (same as making is _set) (f) sorted(List) which is true if List is sorted (increasingly or decreasingly) (g) analyze(List) which displays the message 'This list is sorted', or "This list is not sorted' depending on the values in the list (h) minplusmax(List, MinPlusMax) which returns the sum of the minimum and the maximum element of the List Notes: (1) Performance of these predicates is not critical (all reasonable solutions are OK); (2) Monotonically increasing means-

Explanation / Answer

#include <iostream>

#include <list>

#include <iterator>

using namespace std;

//function for printing the elements in a list

void showlist(list <int> g)

{

    list <int> :: iterator it;

    for(it = g.begin(); it != g.end(); ++it)

        cout << ' ' << *it;

    cout << ' ';

}

int main()

{

    list <int> gqlist1, gqlist2;

    for (int i = 0; i < 10; ++i)

    {

        gqlist1.push_back(i * 2);

        gqlist2.push_front(i * 3);

    }

    cout << " List 1 (gqlist1) is : ";

    showlist(gqlist1);

    cout << " List 2 (gqlist2) is : ";

    showlist(gqlist2);

    cout << " gqlist1.front() : " << gqlist1.front();

    cout << " gqlist1.back() : " << gqlist1.back();

    cout << " gqlist1.pop_front() : ";

    gqlist1.pop_front();

    showlist(gqlist1);

    cout << " gqlist2.pop_back() : ";

    gqlist2.pop_back();

    showlist(gqlist2);

    cout << " gqlist1.reverse() : ";

    gqlist1.reverse();

    showlist(gqlist1);

    cout << " gqlist2.sort(): ";

    gqlist2.sort();

    showlist(gqlist2);

    return 0;

}

#include <iostream>

#include <list>

#include <iterator>

using namespace std;

//function for printing the elements in a list

void showlist(list <int> g)

{

    list <int> :: iterator it;

    for(it = g.begin(); it != g.end(); ++it)

        cout << ' ' << *it;

    cout << ' ';

}

int main()

{

    list <int> gqlist1, gqlist2;

    for (int i = 0; i < 10; ++i)

    {

        gqlist1.push_back(i * 2);

        gqlist2.push_front(i * 3);

    }

    cout << " List 1 (gqlist1) is : ";

    showlist(gqlist1);

    cout << " List 2 (gqlist2) is : ";

    showlist(gqlist2);

    cout << " gqlist1.front() : " << gqlist1.front();

    cout << " gqlist1.back() : " << gqlist1.back();

    cout << " gqlist1.pop_front() : ";

    gqlist1.pop_front();

    showlist(gqlist1);

    cout << " gqlist2.pop_back() : ";

    gqlist2.pop_back();

    showlist(gqlist2);

    cout << " gqlist1.reverse() : ";

    gqlist1.reverse();

    showlist(gqlist1);

    cout << " gqlist2.sort(): ";

    gqlist2.sort();

    showlist(gqlist2);

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote