The usual delete-duplicates, such as the one shipped with SRFI 1,
are standard recursive procedures, that in the worst case have to go
twice over the whole list. It is standard thus to use a hash table to
recall the values we have already seen, or the values we have
already added to the result, and in one single pass over the list
construct the result without duplicates.
This first attempt, has a loop procedure (see
ProcedureRecipeRecursive? to learn how to use named let) that
traverses the list, adding each unseen element to the hash, and it
also adds this new element to the result lists, ans.
We can do better than that by noticing two things:
Both libraries: list.ss from Mzlib, and SRFI 1 provide a procedure named filter, similar to map except that it only collects the results that pass a given test.
When a value is not found in a hash table, a failure thunk is executed, so we can customize this thunk to do what we want, as in: