Suppose I have a function that returns a certain value when it satisfies a given condition and does not return any value when the condition is not satisfied.
e.g.
fun foo(n)= if n< 100000 then n else (something like exit function. We have it in other programming languages. Do we have something like that here?)
I initially wanted to write () but it says mismatch in two conditions of if or something.
Becoming more explicit, I want to write a function that takes a number, any number and combines to a list if it is valid integer and disregards it if it is not integer.
In SML a function always has to return a value¹. One way to represent a function that may or may not have a useful result would be using the
OPTIONtype. UsingOPTIONyour function would returnSOME valueif it has something useful to return, orNONEif it does not.For your particular use-case another alternative would be to take the list as a second argument and then either return the result prepended to the list or the list unchanged.
¹ In fact I’m not aware of any language where a non-void function is allowed to not return a value (without invoking undefined behavior). In some languages you can return
nullin place of a proper return value, but in most languages that isn’t allowed for functions with return typeint. Either way SML doesn’t have a null value.