> Here is a possible solution:

Thanks for this. It prompts me to ask a question that about something

>

> let rec combine x = function

> [] -> []

> | h :: t -> (x, h) :: (combine x t);;

>

> let rec choose_two = function

> [] | [_] -> []

> | h :: t -> (choose_two t) @ (combine h t);;

>

I never fully understood: When to use (like you did in this example)

'function' and when 'match x with'...

I can rewrite your "combine" function like this:

let rec kombine x l = match l with

[] -> []

| h :: t -> (x, h) :: (kombine x t);;

and it seems to do exactly the same.

Regards

Johann

--

Johann Spies Telefoon: 021-808 4036

Informasietegnologie, Universiteit van Stellenbosch

"Trust in the Lord with all your heart, and do not

lean on your own understanding."

Proverbs 3:5

Vrywaring: Jy hoef eintlik net die e-pos self te gelees het. :)

Disclaimer: If you are reading this you are wasting your time :)- to Paola example finding the combinations

let rec combine_2_list in_list out_list =

match in_list with

| [] -> List.rev out_list

| [x; y] :: t -> combine_2_list t

(if t <> [] then

(List.fold_left (fun r elt ->

match elt with

| [x'; y'] -> r @ [(x, x'); (y, y')]

| _ -> r) [] t :: out_list (* or raise exception *))

else out_list)

| _ -> out_list (* or raise exception *) in

combine_2_list [["a"; "b"]; ["c"; "d"]; ["e"; "f"]; ["h"; "k"]] []

;;

On 7/11/05, Eray Ozkural <erayo@...> wrote:

> I would like to note that this might be a perfect problem to

> demonstrate use of lazy evaluation. In most applications

> of the combination function we are merely interested in

> iterating or mapping the elements of the combination

> list, without actually generating anything. Good for a

> tutorial?

>

> On 7/7/05, Lecca Paola <lecca@...> wrote:

> >

> > Dear all,

> > I'm dealing now with the problem of finding all the combinations between

> > the elements of a list of lists. For example, suppose to have

> >

> > let l1 = [ ["a"; "b"]; ["c"; "d"]; ["e"; "f"]; ["h"; "k"] ];;

> >

> > I wish to combine with the function List.combine, the first element of l1

> > with each of the remaining elements, then I want to combine the second

> > element of l1 with each of the successive elements, and so on.

> > The final result I would like to obtain looks like the following

> >

> > [("a", "c"); ("b", "d")]; [("a", "e"); ("b", "f")]; [("a", "h"); ("b",

> > "k")]; [("c", "e"); ("d", "f")]; [("c", "h"); ("d", "k")]; [("e", "h");

> > ("f", "k")] ]

> >

> > However, I'm not able to find a strategy to combine an element of l1 with

> > another element that is not the next, i. e. how to perform the combination

> > between ["a", "b"] and ["e"; "f"], for instance ?

> >

> > Using the function "numberd_list" suggested by Richard Jones I tried to

> > write the following

> >

> > (* numbering a list *)

> > let numbered_list xs =

> > let rec loop n = function

> > | [] -> []

> > | x :: xs -> (n, x) :: loop (succ n) xs

> > in

> > loop 1 xs;;

> >

> >

> > (* get the next element of a given element of a numbered list *)

> > (* the input of this function is the output of the numbered_list *)

> > (* function *)

> >

> > let list_next elt l =

> > match l with

> > [] -> []

> > |_ -> List.nth l ((first elt))

> > ;;

> >

> > let rec combine_in l =

> > let nl = numbered_list l in

> > match nl with

> > [] -> []

> > | nl'::nl ->

> >

> > (List.combine (second nl') (list_next nl' l)):: combine_in l

> > ;;

> >

> > combine_in should combine al least each element with the next one, but

> > also this simple attempt fails.

> >

> > Thanks for your attention,

> > Paola.

> >

> >

> > Archives up to September 30, 2004 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

> >

> >

> >

> >

> >

> >

> >

>

>

> --

> Eray Ozkural (exa), PhD candidate. Comp. Sci. Dept., Bilkent University, Ankara

> http://www.cs.bilkent.edu.tr/~erayo Malfunction: http://www.malfunct.com

> Uludag Project: www.uludag.org.tr KDE Project: http://www.kde.org

>

>

> Archives up to September 30, 2004 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

>

>

>

>

>

>

>