Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How do I compile/run this sample program in OCaml? I am very new to OCaml and having trouble getting started. I have installed ocaml and
How do I compile/run this sample program in OCaml? I am very new to OCaml and having trouble getting started. I have installed ocaml and can compile this program with ocamlopt, but I don't know how to provide the input.
(* binop ('a -> a> 'a) 'a list >a list *) let binop opfunction I>failwith "invalid expression" (* interpfloat list -string -stringfloat list *) let interp s functiorn binop+.) s I "-" -> "subt r " , binop ( -. ) s I "**-> "mult", binop (*. ) s | "/" -> "divide", binop /.) s I "A" -"exp", binop *s | str ->"push", (float of string str) ::s (* interp_and_show:float list -> string let interp_and_show s inp - float list ) let op,s'interp s inp in Printf.print f "%s\t%s\t" np op; List. (iter (Printf.printf "F ") (rev s)) print_newline ); (*rpn_eval:string -> float list ) let rpn-eval str = Printf.printf "Token tAction tstack " let ssStr.(split (regexp string "") str) in List.fold_left interp_and_show [ ss Evaluation of the test expression # rpn, eval "3 4 2 * Token Action Stack 15-23^^/+". push 3 push 3. 4 push 3. 4. 2. mult 3. 8 push 3. 8. 1. push 3. 8. 1. 5 subtr 3. 8. -4. push 3. 8. -4. 2. push 3. 8. -4. 2. 3. exp exp divide 3. 0.0001220703125 add 4 2 2 3. 8. 65536 3.00012207031 float list = [3.0001220703125] (* binop ('a -> a> 'a) 'a list >a list *) let binop opfunction I>failwith "invalid expression" (* interpfloat list -string -stringfloat list *) let interp s functiorn binop+.) s I "-" -> "subt r " , binop ( -. ) s I "**-> "mult", binop (*. ) s | "/" -> "divide", binop /.) s I "A" -"exp", binop *s | str ->"push", (float of string str) ::s (* interp_and_show:float list -> string let interp_and_show s inp - float list ) let op,s'interp s inp in Printf.print f "%s\t%s\t" np op; List. (iter (Printf.printf "F ") (rev s)) print_newline ); (*rpn_eval:string -> float list ) let rpn-eval str = Printf.printf "Token tAction tstack " let ssStr.(split (regexp string "") str) in List.fold_left interp_and_show [ ss Evaluation of the test expression # rpn, eval "3 4 2 * Token Action Stack 15-23^^/+". push 3 push 3. 4 push 3. 4. 2. mult 3. 8 push 3. 8. 1. push 3. 8. 1. 5 subtr 3. 8. -4. push 3. 8. -4. 2. push 3. 8. -4. 2. 3. exp exp divide 3. 0.0001220703125 add 4 2 2 3. 8. 65536 3.00012207031 float list = [3.0001220703125]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started