F Sharp convert list of tuples into list of mapped tuples

f#, functional-programming

Solution

[(2,"c");(1,"a");(2,"b")]
|> List.groupBy fst
|> List.map (fun (x,y)->x,List.map snd y)

Result:

[(2, ["c"; "b"]); (1, ["a"])]

Type inference is handy for the toRel bit:

let toRel xs = 
  xs
  |> List.groupBy fst
  |> List.map (fun (x,y)->x,List.map snd y)

Usage:

toRel [(2,"c");(1,"a");(2,"b")]

Problem

Declare a function that converts a list of pairs to a Relation. ``` type Relation<'a,'b> = ('a * 'b list) list ``` Basically, turn this: ``` [(2,"c");(1,"a");(2,"b")] ``` into this: ``` [(2,["c";"b"]);(1,["a"])] ``` in this form: ``` toRel:(’a*’b) list -> Rel<’a,’b> ``` Any ideas? This isn't homework, just self-study and this one has me a bit stumped considering the form doesn't allow for accumulation.

Original source