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 OPTION
type. Using OPTION
your function would return SOME value
if it has something useful to return, or NONE
if 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 null
in place of a proper return value, but in most languages that isn't allowed for functions with return type int
. Either way SML doesn't have a null value.