2 people like it.

Permutations

computes the list of all permutations of a list for example the permutations of [1;2;3] will be [1;2;3]; [1;3;2]; [2;1;3]; [2;3;1]; [3;1;2]; [3;2;1]

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
    let rec permutations (A : 'a list) =
        if List.isEmpty A then [[]] else
        [
            for a in A do
            yield! A |> List.filter (fun x -> x <> a) 
                     |> permutations
                     |> List.map (fun xs -> a::xs)
        ]
val permutations : A:'a list -> 'a list list (requires equality)

Full name: Script.permutations
val A : 'a list (requires equality)
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.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 isEmpty : list:'T list -> bool

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

Full name: Microsoft.FSharp.Collections.List.filter
val x : 'a (requires equality)
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val xs : 'a list (requires equality)

More information

Link:http://fssnip.net/4t
Posted:13 years ago
Author:Carsten König
Tags: permutation , list