In my previous question yesterday Getting max value from a list in SML, I figured out with the help of Andreas Rossberg how to evaluate a function that gets the Max of a list of integers using SML.
Continuing on to my study. The same code was revised to use Options. Here is the code
fun max1 (xs : int list) =
if null xs
then NONE
else
let val tl_ans = max1(tl xs)
in
if isSome tl_ans andalso valOf tl_ans > hd xs
then tl_ans
else SOME (hd xs)
end
I have the following questions:
- Why does the code inside the else statement output a value of type
option
? - What are the advantages of using
option
s?