Commits

camlspotter committed b248096

added List.fold_left1 and List.sum

  • Participants
  • Parent commits a6c4990

Comments (0)

Files changed (2)

File lib/xlist.ml

   in
   scan acc 0 xs
 
+let fold_left1 f = function
+  | [] -> invalid_arg "fold_left1" (* check the stack trace to see the use site *)
+  | x::xs -> List.fold_left f x xs
+
+let sum xs = List.fold_left (+) 0 xs
+
 module Infix = struct
   let (--) = (--)
 end

File lib/xlist.mli

   -> 'a -> 'b list -> 'a
   (** [foldl] but stoppable *)
 
+val fold_left1 : ('a -> 'a -> 'a) -> 'a list -> 'a
+(** List must be non-empty. 
+    Otherwise, it raises [Invalid_argment "fold_left1"]. *)
+
+val sum : int list -> int
+
 module Infix : sig
   val (--) : int -> int -> int list
   (** [same as from_to. [f--t = [f..t]] ] *)