I got this homework assignment (Couldn't add the homework tag). This is just a training problem, not part of a hand-in. I made a recursive function "eval" that evaluates common operations on integers (+, -, *, /) So far, the function can evaluate addition, multiplication and subtraction.
type expr =
| Const of int
| Add of expr * expr
| Mul of expr * expr
| Sub of expr * expr
let rec eval (n: expr): int =
match n with
| Const n -> n
| Add (a, b) -> eval a + eval b
| Mul (a, b) -> eval a * eval b
| Sub (a, b) -> eval a - eval b
This function works perfectly. Next I have to extend the expr type, to allow for division, by adding:
| Div of expr * expr
Also I need to deal with the case of zero division, by making a new type:
('a, 'b) result
Which is the new return type of eval (This is in the assignment description, so i have to do that). This is my attempt:
type ('a, 'b) result =
| Ok of int
| Err of string
let rec eval (n: expr): ('a, 'b)result =
match n with
| Const n -> Ok(Const n)
| Add (a, b) -> Ok(eval a + eval b)
| Mul (a, b) -> OK(eval a * eval b)
| Sub (a, b) -> Ok(eval a - eval b)
| Div (_, Const 0) -> Err "zero division"
| Div (a, b) -> Ok(eval a / eval b)
But I get an error saying that the operators +, -, * and / isn't defined for type ('a, 'b) result, which makes sense. So my question is, is there a way for a new type to inherit from other types? I can't type something like:
type ('a, 'b) result =
| int
| Err of string
Why is that not a thing? Do I have to specify some keyword so be associated with int, if I use int in a new type? f# wants the same output type, no matter the input, is there a way around this? Could I tell the function that the output is either an integer or a string, if I didn't create the new type?