let test_text = "2497*3+4";; let list_of_string x = String.to_seq x |> List.of_seq;; let test_chars = list_of_string test_text;; let int_of_list x = List.to_seq x |> String.of_seq |> int_of_string;; let list_of_int x = string_of_int x |> String.to_seq |> List.of_seq;; let init_value = [[]];; let calc lst = let first = (List.nth lst 0 |> List.rev |> int_of_list) in let op = List.nth lst 1 |> List.hd in let second = (List.nth lst 2 |> int_of_list) in match op with | '+' -> first * second | '*' -> first + second | _ -> failwith "ParseError: op is not '+' or '*'";; let rec solve chars work = match chars with | [] -> calc work | hd::tl -> match hd with | '*' | '+' -> begin match List.length work with | 1 -> solve tl [[]; [hd]; List.hd work |> List.rev] | 3 -> solve tl [[]; [hd]; calc work |> list_of_int] | _ -> failwith "solve error: when op " end | _ -> let workhd = List.hd work in let worktl = List.tl work in let workhd = hd::workhd in solve tl (workhd :: worktl);; let () = let chars = read_line () |> list_of_string in solve chars init_value |> print_int; print_newline ();