From ab76ce2b6c92c780442b12408839e79a2bf1993a Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Thu, 17 Jun 2010 22:16:18 +0100 Subject: [PATCH] The module stdext/Listext got a safe_head function that wraps hd in an option type. Signed-off-by: Matthias Goergens --- stdext/listext.ml | 4 ++++ stdext/listext.mli | 1 + 2 files changed, 5 insertions(+) diff --git a/stdext/listext.ml b/stdext/listext.ml index 9023569..5f9e1c7 100644 --- a/stdext/listext.ml +++ b/stdext/listext.ml @@ -193,6 +193,10 @@ let rec tails = function | [] -> [[]] | (_::xs) as l -> l :: tails xs +let safe_hd = function + | a::_ -> Some a + | [] -> None + let rec replace_assoc key new_value = function | [] -> [] | (k, _) as p :: tl -> diff --git a/stdext/listext.mli b/stdext/listext.mli index d1f8b1e..66c0729 100644 --- a/stdext/listext.mli +++ b/stdext/listext.mli @@ -176,6 +176,7 @@ module List : val take : int -> 'a list -> 'a list val tails : 'a list -> ('a list) list + val safe_hd : 'a list -> 'a option (** Replace the value belonging to a key in an association list. *) val replace_assoc : 'a -> 'b -> ('a * 'b) list -> ('a * 'b) list -- 2.39.5