2 people like it.

Phone number to words

Creates all possible mobile phone letter combinations from a number.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
let letters = 
    [
    '0', ['0']
    '1', ['1']
    '2', ['A';'B';'C']
    '3', ['D';'E';'F']
    '4', ['G';'H';'I']
    '5', ['J';'K';'L']
    '6', ['M';'N';'O']
    '7', ['P';'Q';'R';'S']
    '8', ['T';'U';'V']
    '9', ['W';'X';'Y';'Z']
    ]
    |> dict

let rec permuteDigits = function
    | "" -> [""]
    | s ->
        let hd, tl = s.[0], s.Substring(1)
        [for s in permuteDigits(tl) do 
            for c in letters.[hd] -> c.ToString() + s]

permuteDigits "3663"
val letters : System.Collections.Generic.IDictionary<char,char list>

Full name: Script.letters
val dict : keyValuePairs:seq<'Key * 'Value> -> System.Collections.Generic.IDictionary<'Key,'Value> (requires equality)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.dict
val permuteDigits : _arg1:string -> string list

Full name: Script.permuteDigits
val s : string
val hd : char
val tl : string
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
val c : char
System.Char.ToString() : string
System.Char.ToString(provider: System.IFormatProvider) : string

More information

Link:http://fssnip.net/dZ
Posted:11 years ago
Author:Phillip Trelford
Tags: puzzle