add CCList.hd_tl

This commit is contained in:
Simon Cruanes 2016-02-23 15:34:26 +01:00
parent 16ac701de1
commit 23e3544adc
2 changed files with 14 additions and 0 deletions

View file

@ -454,6 +454,15 @@ let rec drop n l = match l with
| _ when n=0 -> l
| _::l' -> drop (n-1) l'
let hd_tl = function
| [] -> failwith "hd_tl"
| x :: l -> x, l
(*$T
try ignore (hd_tl []); false with Failure _ -> true
hd_tl [1;2;3] = (1, [2;3])
*)
let take_drop n l = take n l, drop n l
let split = take_drop

View file

@ -106,6 +106,11 @@ val take : int -> 'a t -> 'a t
val drop : int -> 'a t -> 'a t
(** Drop the [n] first elements, keep the rest *)
val hd_tl : 'a t -> 'a * 'a t
(** [hd_tl (x :: l)] returns [hd, l].
@raise Failure if the list is empty
@since NEXT_RELEASE *)
val take_drop : int -> 'a t -> 'a t * 'a t
(** [take_drop n l] returns [l1, l2] such that [l1 @ l2 = l] and
[length l1 = min (length l) n] *)