Helpful

Posted by Steve on June 06, 2004
Final Year Project

On the SML newsgroup someone asked the following

> hello!
> i have think how to do something like this:
> for example i have
> [1,3,2,1,2,4,2,1,3,4]
> and i want to get
> [1,3,2,4]
>
> The idea is to delete repeted elements.
> can someone help me?

The response was

“Recursive thinking”:

1) Write the (trivial) algorithm that handles a one-element list.
2) Write the algorithm that handles a list of N elements. If you take
the list apart into (a) its first element and (b) a rest list of N-1
elements, you can recursively call your function on the rest list, so
all you have to consider now is the case if you have a new element and
ponder whether it should be made part of the list or not.

Consider varying the result. Will the program become simpler if you do
step (1) with a zero-element list?

Feel free to ask more if this doesn’t get you going.

I know how I’d write a function to do that and I don’t think that response was in the least helpful. Infact I’m now slightly more confused. It really does make me wonder sometimes.

No comments yet.

Leave a comment

WP_Big_City