:
let allpairs L =
[for x in L do
for y in L -> (x*y)]
, :
let pairs2 L = L |> List.collect (fun x -> L |> List.map (fun y -> (x*y)))
:
, :
type Microsoft.FSharp.Collections.List<'a> with
member L.cross f =
[for x in L do
for y in L -> f x y]
:
> [1;2;3].cross (fun x y -> (x,y));;
val it : (int * int) list =
[(1, 1); (1, 2); (1, 3); (2, 1); (2, 2); (2, 3); (3, 1); (3, 2); (3, 3)]
F # , # 'ish. , , F # , pipe (| > ).
List , :
module List =
let cross f L1 L2 =
[for x in L1 do
for y in L2 -> f x y]
, cross, List:
> List.cross (fun x y -> (x,y)) [1;2;3] [1;2;3];;
val it : (int * int) list =
[(1, 1); (1, 2); (1, 3); (2, 1); (2, 2); (2, 3); (3, 1); (3, 2); (3, 3)]
> List.cross (*) [1;2;3] [1;2;3];;
val it : int list = [1; 2; 3; 2; 4; 6; 3; 6; 9]