-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.ml
27 lines (23 loc) · 844 Bytes
/
slice.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(*
Extract a slice from a list. (medium)
Given two indices, i and k, the slice is the list containing the elements
between the i'th and k'th element of the original list (both limits included).
Start counting the elements with 0 (this is the way the List module numbers
elements).
*)
let slice list start finish =
let rec slice' list acc current =
if current > finish then acc
else match list with
| [] -> acc
| x :: xs ->
if current < start then slice' xs acc (current + 1)
else slice' xs (x :: acc) (current + 1) in
List.rev (slice' list [] 0);;
slice ["a";"b";"c";"d";"e";"f";"g";"h";"i";"j"] 2 6;;
(* - : string list = ["c"; "d"; "e"; "f"; "g"] *)
slice ["a";"b";"c"] 0 0;;
slice ["a";"b";"c"] (-4) 0;;
slice ["a";"b";"c"] 0 109;;
slice ["a";"b";"c"] 0 (-1);;
slice ["a";"b";"c"] 2 4;;