Wow, somehow I actually got it. Funny that I'd been working on it for hours, and then like 30 minutes after posting I came up with a solution:

That being said, it's not terribly efficient. If someone could suggest any optimizations I'd appreciate it. I'm pretty sure it's correct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
let match_multi (templates:string list) (words:string list) : (string list list) = 
    let rec match_multi (state:BiDirectionalMap) templates words builder = 
        match templates with
        | [] -> []::[]           //No words can possibly match an empty template
        | [this_template] -> 
            //If there's only one template, just filter out everything that doesn't match it
            let result = words
                        |> List.map (fun word -> (word,match_template_wrap this_template word state))
                        |> List.filter (fun (s,(b,m)) -> b)
                        |> List.map (fun (s,(_,_)) -> s)
            (List.map (fun x -> x::builder) result)
        | this_template::rest -> 
            //All entries in this list matched the template, and produced a new state map
            //For each entry in the list, remove those that do not match this template
            words
            //Match each word against the current template
            |> List.map (fun word -> (word,match_template_wrap this_template word state))
            //Remove those items that did not match this template
            |> List.filter (fun (s,(b,m)) -> b)
            //Get rid of the match boolean flag since they will always be true now
            //For each item that matched this template, take its new map and test
            //the rest of the templates against this new map
            |> List.map_concat (fun (word,(_,map)) -> 
                            let new_words = List.filter ((<>) word) words
                            match_multi map rest new_words (word::builder))
            |> Set.of_list |> Set.to_list
    match_multi ([], []) templates words []
By on 1/28/2009 6:26 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper