Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

If i have a list of pairs ex. (String, int) how to add every int in strings are the same to one single pair

$
0
0

i have this problem where i have a list of pairs string int and i want to sum the total of ints with the same String ex:list -> [("a",1);("b",1);("a",1);("c",1)] should returnlist -> [("a",2);("b",1);("c",1)] order doesnt mather will sort later´

for now i have this

let rec merge l =   match l with  | [] -> []  | (c1,n1)::(c2,n2)::xs ->     if c1 = c2 then      (c1, n1+n2)::merge xs    else      something i cant think of yet

thats my train on thougth but i know it wont work yet

ps i cant use imperative stuff suported by ocaml


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>