List Info

Thread: "ocaml_beginners"::[] Re: Generate the combinations of K distinct objects chosen from an N-element l




"ocaml_beginners"::[] Re: Generate the combinations of K distinct objects chosen from an N-element l
user name
2006-11-12 13:03:44

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, "Ulrich" <ulrivo...> wrote:
&gt;
> But this is not so beauty and readable as the Prolog solution:

Well, maybe you think that because you're new to ocaml. Your
&quot;next_vect" function looks quite readable and short to me.

here's how I would do it (without having to define a new type) :

let rec combinations k l=
if k=0 then [[]] else
if List.length(l)<k then [] else
let head=List.hd(l) and tail=List.tl(l) in
let first_part_of_answer=List.map(function y->head::y)(combinations (k-1) tail)
and second_part_of_answer=combinations(k)(tail) in
first_part_of_answersecond_part_of_answer

This code optimizes readibility, not performance.

Hope this helps ...

Ewan

__._,_.___
.

__,_._,___
[1]

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