On this page, you will learn a recursive technique to remove duplicates from a list.
Suppose you have a list of items and want to know if the elements of the list are distinct. For example, you might want to make sure that you don't have anything on your shopping list twice.
As a first step, we'll just answer the yes/no question: are there any duplicates on your list?
are the items distinct?
predicate, which is started below. Since you're writing a predicate, your procedure should always report true
or false
.report
block at the bottom, what does that tell you about the items in the list?For your grocery list, you wouldn't just want to know whether or not there are duplicates. You'd want a new list with the duplicates removed.
distinct items from
reporter using a structure similar to that of the are the items distinct?
predicate.
The algorithm for this block will make the same decisions as in are the items distinct?
. But that was a predicate. This one has to report a list. So look at the three report
blocks in your code for the are the items distinct?
predicate, and decide what they should report for the distinct items from
reporter.
distinct items from
report?report
block, what does that tell you about the first item in the list? Do you want the first item as part of the list you report?