Source file nonempty_list.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
open! Base

type 'a t = 'a * 'a list

let map (hd, tl) ~f = f hd, List.map ~f tl
let to_list (hd, tl) = hd :: tl
let append (hd, tl) l = hd, tl @ l

let drop_last (hd, tl) =
  match tl with
  | [] -> []
  | _ -> hd :: List.drop_last_exn tl
;;

let last (hd, tl) =
  match tl with
  | [] -> hd
  | tl -> List.last_exn tl
;;