Skip to content

Instantly share code, notes, and snippets.

@opavlyshak
Created December 18, 2012 09:58
Show Gist options
  • Save opavlyshak/4326760 to your computer and use it in GitHub Desktop.
Save opavlyshak/4326760 to your computer and use it in GitHub Desktop.
Find string permutations in F#
let stringPermutations (s : string) =
let rec permute result rest =
match rest with
| "" -> [result]
| _ ->
rest
|> Seq.mapi (fun i -> fun c ->
permute (result + c.ToString()) (rest.Remove(i, 1)))
|> Seq.concat
|> Seq.toList
permute "" s
let testStringPermutationsCount s =
let factorial n =
let rec factorialIter result current =
match current with
| _ when current > n -> result
| _ -> factorialIter (result * current) (current + 1)
factorialIter 1 1
let permutations = Set.ofList (stringPermutations s)
permutations.Count = factorial s.Length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment