List Info

Thread: "ocaml_beginners"::[] removing each of a list's elements in turn




"ocaml_beginners"::[] removing each of a list's elements in turn
user name
2006-11-28 20:51:39

I want to take a list and return the list of lists gained by removing
each of its elements in turn, so that e.g. [1;2;3;4] => [2;3;4];
[1;3;4];[1;2;4];[1;2;3]. How do I do this neatly and efficiently? Is
there a better data structure than a list over which to do this? (The
only other operation I need is linear iteration).

martin

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: removing each of a list's elements in turn
user name
2006-11-28 21:00:07

On 11/29/06, Martin DeMello < martindemello%40gmail.com">martindemellogmail.com> wrote:
&gt; I want to take a list and return the list of lists gained by removing
> each of its elements in turn, so that e.g. [1;2;3;4] => [2;3;4];
> [1;3;4];[1;2;4];[1;2;3]. How do I do this neatly and efficiently? Is
> there a better data structure than a list over which to do this? (The
> only other operation I need is linear iteration).

Okay, should've searched a little longer first!
http://www.lri.fr/~filliatr/ftp/ocaml/misc/anagram.ml.html is solving
pretty much the same problem I am, and in pretty much the same way, so
I'll just steal his multiset implementation

martin

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] removing each of a list's elements in turn
user name
2006-11-28 21:17:48

Linear iteration == List.iter?

let rec sublists l = function
| [] -> []
| x :: xs -> (l xs) :: sublists (l [x]) xs

sublists [] [1;2;3;4] = [[2;3;4];[1;3;4];[1;2;4];[1;2;3]]

Though a naive pass that may require optimisations.

Jonathan

On 11/29/06, Martin DeMello < martindemello%40gmail.com">martindemellogmail.com> wrote:
&gt; I want to take a list and return the list of lists gained by removing
> each of its elements in turn, so that e.g. [1;2;3;4] => [2;3;4];
> [1;3;4];[1;2;4];[1;2;3]. How do I do this neatly and efficiently? Is
> there a better data structure than a list over which to do this? (The
> only other operation I need is linear iteration).
>
&gt; martin
&gt;
>
&gt; Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> Yahoo! Groups Links
>;
>
&gt;
>

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] removing each of a list's elements in turn
user name
2006-11-29 12:47:09

On 11/29/06, Jonathan Roewen < jonathan.roewen%40gmail.com">jonathan.roewengmail.com> wrote:

> let rec sublists l = function
> | [] -> []
> | x :: xs -> (l xs) :: sublists (l [x]) xs
>
> sublists [] [1;2;3;4] = [[2;3;4];[1;3;4];[1;2;4];[1;2;3]]

That's pretty neat - thanks!

martin

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )