bugfix in CCList.take

This commit is contained in:
Simon Cruanes 2014-05-20 17:27:00 +02:00
parent e61039152f
commit a8dc42024b

View file

@ -144,7 +144,10 @@ let take n l =
let rec direct i n l = match l with let rec direct i n l = match l with
| [] -> [] | [] -> []
| _ when i=0 -> safe n [] l | _ 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 and safe n acc l = match l with
| [] -> List.rev acc | [] -> List.rev acc
| _ when n=0 -> List.rev acc | _ when n=0 -> List.rev acc
@ -152,6 +155,12 @@ let take n l =
in in
direct _direct_depth n l 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 let rec drop n l = match l with
| [] -> [] | [] -> []
| _ when n=0 -> l | _ when n=0 -> l