name = 'rev/2' slug = 'reverse a list' description = '''\
rev(L1, L2)
: the list L2
is obtained from L1
by reversing the order of the elements.
?- rev([1,2,3], X). X = [3,2,1]. ?- rev([], X). X = [].''' plan = ['''\
This is one of the most rewarding exercises. Classic recursion! Try to reduce the problem into a smaller one. That, of course, means reducing it to a shorter list.
''', '''\I divide the list into its head and tail, the recursion reverses the tail, and all I have to do is to insert the head into its proper location in the reversed tail.
''', '''\If the given list L
is composed of head H
and tail T
and if I assume the recursion reverses tail T
into reversed tail RT
and if I add head
H
at the end of RT
, then the result is reversed list L
.
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). Perhaps by using =
you can make the predicate rev/2
more general (e.g. able to work with output arguments becoming inputs).
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 is the most trivial to reverse?
''', 'recursive_case': '''\The base case is ok. However, what about the general recursive case?
''', 'predicate_always_false': '''\It seems your predicate is
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 N
is equal to N + 1
,
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?
''', 'base_case_at_len1': '''\Your base case is perfectly reasonable, but it doesn't work for an empty list. However, don't use two base cases here, because this will duplicate the solutions./p> ''', 'arbitrary_base_case': '''\
What is the result of reversing an empty list? Surely not an arbitrary list (a variable without an assigned value)!
''', 'forcing_result_onto_recursion': '''Don't force the result onto recursion, don't tell it what it should return. Just assume it will do its job. If this assumption is correct, then the rule will work for a larger case.
Is your recursive call of the form rev(T, [RevTail|H])
? This forces the recursive call to
return the head at the end of the list. But it doesn't know of this head, because you just
took it away! Inserting the head into the result, returned by the recursive call, is your job. To put it shortly,
insert H
outside of the recursive call.
Predicates last/2
, shiftleft/2
, or shiftright/2
will not be of
much help here. Rather try without them, it will be easier.
Did you insert the head at the start of the reversed tail? That's not its proper place; in this way you just reassemble the original list.
''', 'invalid_insert_at_end': '''\Remember, a list's tail is always another list, not an element. How do you insert an element at the end of the list?
''', 'conc_arg_not_list': '''\All three arguments of predicate conc/3
are lists.
Are you sure you used it properly?