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
Post a Comment