3 people like it.

Cross Join Lists

Given a list of lists, the function crossJoin outputs all combination of elements from the inner lists, i.e. each combination is a list which contains elements from each of the inner lists in the input. Note: Order is not preserved

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
let crossJoin xss = 
    let rec outerListLoop acc xs =
        match xs with
        | [] -> acc
        | x::rest -> innerListLoop acc x rest
    and innerListLoop acc ys xs =
        match ys with
        | []    -> acc
        | y::rest -> 
            let acc' = (y::List.head acc)::List.tail acc
            let os = outerListLoop acc' xs
            let is = innerListLoop acc rest xs 
            os @ if rest |> List.isEmpty then List.tail is else is
    let xss' = xss |> List.filter (fun xs -> List.length xs > 0)
    outerListLoop [[]] xss'


(*
let data = [[1;2];[3;7];[4;5;6]] 
crossJoin data
val it : int list list =
  [[4; 3; 1]; [5; 3; 1]; [6; 3; 1]; [4; 7; 1]; [5; 7; 1]; [6; 7; 1]; [4; 3; 2];
   [5; 3; 2]; [6; 3; 2]; [4; 7; 2]; [5; 7; 2]; [6; 7; 2]]
*)
val crossJoin : xss:'a list list -> 'a list list

Full name: Script.crossJoin
val xss : 'a list list
val outerListLoop : ('b list list -> 'b list list -> 'b list list)
val acc : 'b list list
val xs : 'b list list
val x : 'b list
val rest : 'b list list
val innerListLoop : ('b list list -> 'b list -> 'b list list -> 'b list list)
val ys : 'b list
val y : 'b
val rest : 'b list
val acc' : 'b list list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val head : list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.head
val tail : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.tail
val os : 'b list list
val is : 'b list list
val isEmpty : list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.isEmpty
val xss' : 'a list list
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val xs : 'a list
val length : list:'T list -> int

Full name: Microsoft.FSharp.Collections.List.length

More information

Link:http://fssnip.net/r2
Posted:2 years ago
Author:Faisal Waris
Tags: list , join