Scanf.scanf "%d" (fun n -> let rec loop i stack = if i = n then List.hd stack else let stack = match Scanf.scanf " %s" (fun s -> s) with | "+" -> ( match stack with | x :: y :: tl -> (x + y) :: tl | _ -> stack ) | "-" -> ( match stack with | x :: y :: tl -> (y - x) :: tl | _ -> stack ) | a -> int_of_string a :: stack in loop (i + 1) stack in loop 0 [] |> Printf.printf "%d\n" )