How about

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
 

open System.Collections.Generic 

let dict1 = new Dictionary<string,int>()
dict1.Add("A",1)
dict1.Add("B",2)
dict1.Add("C",3)
dict1.Add("D",4)

let dict2 = new Dictionary<string,int>()
dict2.Add("A",1)
dict2.Add("B",2)
dict2.Add("C",2)

let diff =
    let r = new Dictionary<_,_>()
    for KeyValue(k,v) in dict1 do
        match dict2.TryGetValue k with
        | true, v2 when v2 = v -> ()
        | _ -> r.Add(k,v)
    r

printfn "%A" (diff |> Seq.toList)        

By on 3/3/2010 2:05 PM ()

A more functional style would be like this:

1
2
3
4
5
6
7
8
 

let a = Map.ofList [("a",1);("b",2);("c",3)]
let b = Map.ofList [("a",1);("b",2);("c",2);("d",4)]

let c = Map.filter (fun k v -> not (a.ContainsKey(k)) || a.[k] <> v) b

By on 3/4/2010 5:42 PM ()

Hi, Gary:
Thanks, your code works, and it is more functional.
However, I want another function:
let dict1 = new Dictionary<string,int>()
dict1.Add(“A”,1)
dict1.Add(“B”,2)
dict1.Add(“C”,3)
dict1.Add(“D”,4)
let dict2 = new Dictionary<string,int>()
dict2.Add(“A”,1)
dict2.Add(“B”,2)
dict2.Add(“C”,2)
Let's assume the number of elements in dict1 >= dict2.
I want to have a new dictionary with the number of elements in dict1, the key is the key from dict1, the value is the difference between dict1 and dict2, for those elements appear in dict1 but not in dict2, the value is from dict1.
For my above example:
the new dict3 = {[A,0];[B,0];[C,1]};{[D,4]}.
Let me know what functional code you can have for this.
I think for BrianMCN's code, this seems to be easier to reach this, right?
Another issue: I need the dictionary, but you used list.
Could you please show me the code using dictionary?
Thanks,

By on 3/6/2010 2:00 AM ()
1
2
3
4
5
6
let a = Map.ofList [("a",1);("b",2);("c",3)]
let b = Map.ofList [("a",1);("b",2);("c",2);("d",4)]

let c = Map.map (fun k v -> match a.TryFind k with | None -> v | Some x -> v - x) b

I use a list to load a Map(Dict), you can use other method to load it(use intellisense to find out about a Map data type) but load with list/seq is actually easier from a broad sense when you think about how you get your source( |> from some other function etc.)

By on 3/6/2010 9:35 AM ()

and if you must have the .NET Dictionary

1
2
3
4
5
 

let m2d m = Map.fold (fun (a:Dictionary<_,_>) k v -> a.Add(k,v);a) (new Dictionary<_,_>()) m

though if you works in F# only environment, Map is dictionary.

By on 3/6/2010 10:27 AM ()

Hi, Gary:
Thank you very much, your code works.
However, it is not easy for me to use your code: I am not an expert in functional program. And my start point here is 2 dictionaries, but your start point here is 2 maps, since I don't know how to change dictionary to map, so I don't know how to use your code.
Hope you understand my headache.
But thank you very much for your help and code!
Could you show me how to use your code starting from my dictionaries?
Thank you very much!

By on 3/6/2010 1:20 PM ()

you don't need to use my code, you can use Brian's. If you spend a little bit of time just looking at the intellisense of a dictionary and map, you may be able to come up with the following. The nice thing about F# is its type system would guide you.

1
2
3
let d2m d = d |> Seq.map (fun (x:KeyValuePair<_,_>) -> (x.Key,x.Value)) |> Map.ofSeq

By on 3/6/2010 7:33 PM ()

Hi, Gary:
Thank you for your code.
I will try to change my program to use Map, not the dictionary.
But it will take some time.
Thanks for your help.

By on 3/9/2010 3:43 PM ()

Hi, Brianmcn:
Thank you very much, your code works.
Thanks again!

By on 3/3/2010 2:24 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