type exp = Int of int | Add of exp * exp | Sub of exp * exp

let rec str_of_exp = function
  | Int i -> string_of_int i
  | Add (e1, e2) -> Printf.sprintf "%s+%s" (str_of_exp e1) (str_of_exp e2)
  | Sub (e1, e2) -> Printf.sprintf "%s-%s" (str_of_exp e1) (str_of_exp e2)

let rec calc = function
  | Int i -> i | Add (e1, e2) -> calc e1 + calc e2
  | Sub (e1, e2) -> calc e1 - calc e2

let solve inputs =
  let rec inner tmp = function
    | [] -> if calc tmp = 7 then Some tmp else None
    | l::ls ->
       match inner (Add (tmp, Int l)) ls with
       | Some tmp -> Some tmp
       | None ->
          match inner (Sub (tmp, Int l)) ls with
          | Some tmp -> Some tmp
          | None -> None
  in inner (Int (List.hd inputs)) (List.tl inputs)

let c_to_d c = Char.code c - Char.code '0'
let _ =
  let inputs = Scanf.scanf "%c%c%c%c\n" 
                 (fun a b c d -> 
                   (c_to_d a)::(c_to_d b)::(c_to_d c)::(c_to_d d)::[]) in
  match solve inputs with
  | None -> assert false
  | Some tmp -> (str_of_exp tmp) ^ "=7" |> print_string
