I am trying to learn F#. And I need som help with a simple soundex expression. I am using the following ruleset for Simplified (also called American) soundex:
1.) Assign characters to classes
2.) Remove duplicate values here, e.g. 222 becomes 2
3.) Replace first encoded char with first char
4.) Remove nulls
5.) Truncate ot pad to totally 4 characters
Currently I am stuck on rule no. 2. I was thinking of using a recursive expression. As I am currently a n00b on F# I will try to ask you for an elegant solution to my problem.Maybe my entire approach to translate text to soundex is off target?
Any suggestions will be greatly appreciated :)
Here is my code:
let Simplified (name:string) =
let ca = name.ToLower().ToCharArray()
new string(
Array.map(
fun e ->
match e with
| 'a' | 'e' | 'i' | 'o' | 'u' | 'y' | 'w' | 'h' -> '0'
| 'b' | 'f' | 'p' | 'v' -> '1'
| 'c' | 's' | 'k' | 'g' | 'j' | 'q' | 'x' | 'z' -> '2'
| 'd' | 't' -> '3'
| 'l' -> '4'
| 'm' | 'n' -> '5'
| 'r' -> '6'
| _ -> ' '
) ca
//|> fun s -> TODO: Remove duplicates here
|> fun s -> Array.set s 0 (ca.[0])
Array.choose(fun e -> if e <> '0' then Some(e) else None) s
)
|> fun s -> (
match s.Length with
| x when x < 3 -> s.PadRight(4, '0')
| _ -> s.Substring(0, 4)
).ToUpper()