I have working expression however when I try to evaluate my expressions with my current evaluate function I get following error-- "Error: This pattern matches values of type 'a * 'b but a pattern was expected which matches values of type args". Could you help me how to fix it.
type expr = Const of int
| Var of string
| Plus of args
| Mult of args
| Minus of args
| Div of args
and args = {arg1:expr; arg2:expr};;
let rec eval = function
| Const c -> c
| Plus (f, g) -> eval f + eval g
| Minus(f, g) -> eval f - eval g
| Mult(f, g) -> eval f * eval g
| Div(f, g) -> eval f / eval g
;;
Constructors like
Plusas you have declared it take arguments that are parenthesized. You appear to be using the syntax appropriate to a record type (with curly braces and field names). But there are no record types in your definition ofexpr.Here is a valid value of type
expr:Your
evalfunction doesn't handle the evaluation of variables (theVarconstructor). This is only a warning, not an error, but it will cause a runtime exception if you try to evaluate something likeVar "abc".You don't say which of these errors you're talking about, but I hope this is helpful nonetheless.
Update
As @CraigFe at discuss.ocaml.org pointed out, you have a mismatch between the definition of
exprand the test case. You could rewrite your definition ofexprso that the test case works, or you could rewrite the test case so it works with your current definition ofexpr.To match the test case, you would want a definition like this:
Then you can have a value like this:
However, you can't have field names starting with a capital letter in OCaml. This means that
Arg1andArg2need to bearg1andarg2. So personally I would suspect that the test case is the part that needs revision.I don't understand the part about the mutually recursive definitions (though I know what those are of course). Generally speaking I'd say your biggest difficulty is with the problem statement, not your code.