# HG changeset patch # User Rob Hoes CP-1622: Listext function to replace the value for a key in an assoc list Signed-off-by: Rob Hoes diff -r 3e1beafe76b5 stdext/listext.ml --- a/stdext/listext.ml Tue Feb 16 22:40:11 2010 +0000 +++ b/stdext/listext.ml Tue Feb 16 22:47:58 2010 +0000 @@ -192,4 +192,13 @@ let rec tails = function | [] -> [[]] | (_::xs) as l -> l :: tails xs + +let rec replace_assoc key new_value = function + | [] -> [] + | (k, _) as p :: tl -> + if k = key then + (key, new_value) :: tl + else + p :: replace_assoc key new_value tl + end diff -r 3e1beafe76b5 stdext/listext.mli --- a/stdext/listext.mli Tue Feb 16 22:40:11 2010 +0000 +++ b/stdext/listext.mli Tue Feb 16 22:47:58 2010 +0000 @@ -177,4 +177,6 @@ val tails : 'a list -> ('a list) list + (** Replace the value belonging to a key in an association list. *) + val replace_assoc : 'a -> 'b -> ('a * 'b) list -> ('a * 'b) list end