General
Material
Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5

Solution

As before there will be a 2-place wrapper which calls the 3-place recursive predicate.

What the 3-place predicate does can be described as follows: It takes as input a Prolog term (list or not) and a list (the accumulator), flattens the input term and concatenates the result, which will be a list, with the accumulator.

That means: if the input is the empty list, the accumulator will be returned as the result. If the input is a Prolog term which is not a list, it will be attached to the front of the accumulator. And if the input is a non-empty list, the tail of the list will be flattened. Then the head of the list will be flattened and the result will be concatenated with the result of flattening the tail. This can be achieved by using the result of flattening the tail of the list as accumulator for the call that flattens the head of the list.

Solution

Back to the exercise.