let string_of_list to_s list = "[" ^ String.concat ", " (List.map to_s list) ^ "]"let print_list to_s list = print_newline (print_string (string_of_list to_s list))let diff list = List.map2 (-) (List.tl list) (List.rev (List.tl (List.rev list)))let diff_rec list = let rec aux acc = function | [] -> acc | _ :: [] -> acc | a :: (b :: _ as tail) -> aux (b - a :: acc) tail in List.rev (aux [] list)let () = let p = print_list string_of_int in p (diff [3;1;4;1;5;9;2;6;5]); p (diff_rec [3;1;4;1;5;9;2;6;5])
Standard input is empty
[-2, 3, -3, 4, 4, -7, 4, -1] [-2, 3, -3, 4, 4, -7, 4, -1]
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!