name = 'isort/2' slug = 'sort a list using insertion sort' description = '''\
isort(L, SL)
: the list SL
contains the elements of L
sorted in non-decreasing order. Use the predicate sins/3
to implement insertion sort.
?- isort([2,3,1,5,4], L). L = [1,2,3,4,5].''' plan = ['''\
When going through the list (actually when returning from recursion) at every step insert the current element in its proper position.
''', '''\When going through the list at every step take away the head (it's stored on stack), while its tail goes into recursion (the problem/list is shorter, so this is possible). The recursion returns the sorted tail, and all that's left for you to do is to put the previously taken away head into its proper place in the sorted tail. Of course you can reuse some previous exercise for this task.
''', '''\If list L
is composed of head H
and tail T
and if we assume that
tail T
is correctly sorted into SortedTail
by recursion, and if head H
is inserted into its proper place within SortedTail
, then we get the whole list L
properly sorted.
The operator ==
is "stricter" than operator =
in the sense that
for the latter it is enough to be able to make the two operands equal (unification).
Of course, you can also solve the exercise without explicit use of either of these two operators, just remember that unification is implicitly performed with the predicate's arguments (head of clause).
''', 'eq_instead_of_equ_markup': '''\Perhaps the operator for unification (=) would be better?
''', 'base_case': '''\Did you think of a base case? Which list can you sort without any effort whatsoever?
''', 'recursive_case': '''\The base case is ok. However, what about the general recursive case?
''', 'predicate_always_false': '''\It seems your predicate is always "false". Did you give it the correct name, or is it perhaps misspelled?
If the name is correct, check whether something else is misspelled, perhaps there is a full stop instead of a comma or vice versa, or maybe you typed a variable name in lowercase?
It is, of course, also possible that your conditions are too restrictive, or even impossible to satisfy
(as would be, for example, the condition that X
is simultaneously smaller and greater than
Y
, or something similarly impossible).
Is there an infinite recursion at work here? How will it ever stop?
Or perhaps is there a missing, faulty, or simply incompatible (with the general recursive case) base case?
''', 'min_used': '''\Try solving this exercise without using the predicate min/2
.