Created
December 18, 2012 09:58
-
-
Save opavlyshak/4326760 to your computer and use it in GitHub Desktop.
Find string permutations in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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