How to take product of two list in OCaml? -


i have 2 lists :

let = ["a";"b"]; let b = ["c";"d"]; 

i want output list c such :

c = ["a";"c";"a";"d";"b";"c";"b";"d"]; 

how in ocaml lists immutable? new it.

you return new list. if interested in cartesian product of lists, should enough:

let cartesian l l' =    list.concat (list.map (fun e -> list.map (fun e' -> (e,e')) l') l)  # cartesian ["a";"b"] ["c";"d"];; - : (string * string) list = [("a", "c"); ("a", "d"); ("b", "c"); ("b", "d")] 

if need strange flat structure instead, can use additional list concatenation.

let flat_cartesian l l' =    list.concat (list.concat (     list.map (fun e -> list.map (fun e' -> [e;e']) l') l)) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -