name = 'sublist/2' slug = 'generate sublists of a list' description = '''\
sublist(L, SL)
: SL
is a continuous sublist of
list L
. Your program should return every possible sublist; each
answer may be returned more than once.
?- sublist([1,2,3], X). X = [] ; X = [1] ; X = [1,2] ; X = [1,2,3] ; X = [2] ; X = [2,3] ; X = [3].''' plan = ['''\
First a reminder: we're looking for sublists, not subsets. The difference is that sublists contain a number of consecutive elements of the original list.
Perhaps you should look for some pattern? And which predicate is ideal to search for patterns? You already know that. ;)
''', '''\Of course, predicate conc/3
can be used to search for patterns in lists. Perhaps this time
we don't even need explicite recursion? Is that really possible in prolog? ;)
So, what could the pattern be? Well, a part of the original list! Imagine that the original list is a tube that you want to shorten -- a little bit from the left, a little bit from the right -- what's left is a sublist! You chop off some elements from the front of the original list, and then you chop off some from the end.
'''] hint = { 'eq_instead_of_equ': '''\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 sublist/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?
''', '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?
''', }