I want to ask the user to input a variable and check it is real or integer and take two different operations for corresponding actions. Say true if integer else false;
fun realorinteger(n)= if n=int then true else false;
but it definitely does not work. I tried if n in int as well.
Any help?
You cannot do this.
The type system simply doesn’t allow a function to take multiple different types, and act according to which type it is. Either your function takes an
int, or it takes areal. (Or it takes both, but can also takestrings,lists, etc… ie. is polymorphic)You could fake it by making a datatype, which encapsulates values that can be either integers or reals, like so:
You can then use pattern matching on such a value to extract the desired number:
This function will then have the type
intorreal -> x, wherexis the type of the right-hand-side expressions. Note, the resulting value must be of the same type in both cases.An example of such a function could be a rounding function:
Which is then called like so: