AIMA Artificial Intelligence a modern approch

AIMA-exercises is an open-source community of students, instructors and developers. Anyone can add an exercise, suggest answers to existing questions, or simply help us improve the platform. We accept contributions on this github repository.

Exercise 9.20

This exercise looks at sorting in Prolog.

  1. Write Prolog clauses that define the predicate sorted(L), which is true if and only if list L is sorted in ascending order.

  2. Write a Prolog definition for the predicate perm(L,M), which is true if and only if L is a permutation of M.

  3. Define sort(L,M) (M is a sorted version of L) using perm and sorted.

  4. Run sort on longer and longer lists until you lose patience. What is the time complexity of your program?

  5. Write a faster sorting algorithm, such as insertion sort or quicksort, in Prolog.

View Answer