Cartesian product of two or more lists: Difference between revisions
Content added Content deleted
m (→{{header|FreeBASIC}}: remove a bunch of unused variables) |
|||
Line 2,381: | Line 2,381: | ||
(*- : ('a * int) list = []*)</lang> |
(*- : ('a * int) list = []*)</lang> |
||
Implementation with a bit more tail-call optimization, |
Implementation with a bit more tail-call optimization, introducing a helper function. The order of the result is changed but it should not be an issue for most uses. |
||
<lang ocaml>let product' l1 l2 = |
<lang ocaml>let product' l1 l2 = |
||
let rec aux ~acc l1' l2' = |
let rec aux ~acc l1' l2' = |
||
Line 2,411: | Line 2,411: | ||
(*- : ('a * int) list = [] *)</lang> |
(*- : ('a * int) list = [] *)</lang> |
||
Extra credit function. Since in OCaml a function can return only one type, and because tuples of different arities are different types, this returns a list of lists rather than a list of tuples. |
Extra credit function. Since in OCaml a function can return only one type, and because tuples of different arities are different types, this returns a list of lists rather than a list of tuples. Since lists are homogeneous this version is restricted to products over a ''single'' type, eg integers. |
||
<lang ocaml>let rec product'' l = |
<lang ocaml>let rec product'' l = |
||
(* We need to do the cross product of our current list and all the others |
(* We need to do the cross product of our current list and all the others |