Anonymous user
Cartesian product of two or more lists: Difference between revisions
Cartesian product of two or more lists (view source)
Revision as of 18:05, 12 December 2020
, 3 years ago→{{header|OCaml}}
m (→{{header|FreeBASIC}}: remove a bunch of unused variables) |
|||
Line 2,381:
(*- : ('a * int) list = []*)</lang>
Implementation with a bit more tail-call optimization,
<lang ocaml>let product' l1 l2 =
let rec aux ~acc l1' l2' =
Line 2,411:
(*- : ('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. Since lists are homogeneous this version is restricted to products over a ''single'' type, eg integers.
<lang ocaml>let rec product'' l =
(* We need to do the cross product of our current list and all the others
|