5 people like it.

Function to generate all possible combinations where combination "ab" != "ba"

Function to generate all possible combinations where combination "ab" is different then "ba"

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
open System

let append (a:string) (lst:string list) = 
    lst |> List.collect (fun ch -> if a.Contains(ch) then [] else [a+ch])

let generateCombination (lst:string list) = 
    let total = lst.Length
    let rec combination (acc:string list list) (src:string list) (target: string list) = 
        let result = [for i in src -> target |> append i] |> List.collect id
        match result.Head.Length with
        | x when x = total -> result::acc
        | _ -> combination (result::acc) result target

    combination [lst] lst lst
  
for str in generateCombination [for i in "abc" -> i.ToString()] |> List.rev |> List.collect id do
    str |> Console.WriteLine 

// Input: "ab"
// output: "a" "b" "ab" "ba"
namespace System
val append : a:string -> lst:string list -> string list

Full name: Script.append
val a : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val lst : string list
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 collect : mapping:('T -> 'U list) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.collect
val ch : string
String.Contains(value: string) : bool
val generateCombination : lst:string list -> string list list

Full name: Script.generateCombination
val total : int
property List.Length: int
val combination : (string list list -> string list -> string list -> string list list)
val acc : string list list
val src : string list
val target : string list
val result : string list
val i : string
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id
property List.Head: string
property String.Length: int
val x : int
val str : string
val i : char
Char.ToString() : string
Char.ToString(provider: IFormatProvider) : string
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
Raw view New version

More information

Link:http://fssnip.net/1S
Posted:6 years ago
Author:Ankur Dhama
Tags: list , recursion , combination