From a8dc42024b2316987ee55651beaee6e128e9fc54 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 20 May 2014 17:27:00 +0200 Subject: [PATCH] bugfix in CCList.take --- core/CCList.ml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/CCList.ml b/core/CCList.ml index e368b280..0d446ed8 100644 --- a/core/CCList.ml +++ b/core/CCList.ml @@ -144,7 +144,10 @@ let take n l = let rec direct i n l = match l with | [] -> [] | _ when i=0 -> safe n [] l - | x::l' -> x :: direct (i-1) (n-1) l' + | x::l' -> + if n > 0 + then x :: direct (i-1) (n-1) l' + else [] and safe n acc l = match l with | [] -> List.rev acc | _ when n=0 -> List.rev acc @@ -152,6 +155,12 @@ let take n l = in direct _direct_depth n l +(*$T + take 2 [1;2;3;4;5] = [1;2] + take 10_000 (range 0 100_000) |> List.length = 10_000 + take 10_000 (range 0 2_000) = range 0 2_000 +*) + let rec drop n l = match l with | [] -> [] | _ when n=0 -> l