How to transform the "real option" to real?
And the "SOME" means what?
> - val x = Real.fromString("12.4");
> val x = SOME 12.4 : real option
> - val y = 12.3;
> val y = 12.3 : real
>
> How to transform the "real option" to real?
Use pattern matching (case expressions).
> And the "SOME" means what?
If you pass a string which doesn't parse to a real number to
Real.fromString, the function returns "NONE". If the number was
parsed successfully, it returns "SOME v", where "v" is that number.
(SOME and NONE are data constructors of the option type).
Consider the following SML session:
- val x = Real.fromString("12.4");
> val x = SOME 12.4 : real option
- val y = Real.fromString("This isn't a number");
> val y = NONE : real option
- val real_x = valOf x;
> val real_x = 12.4 : real
- val real_y = valOf y;
! Uncaught exception:
! Option
In summary, the option type allows for a value to be present or
absent while still being able to be represented with ML's strong
typing. In this case, Real.fromString returns an option type to
accommodate the case when the string it is given does not represent
a number (y above).
Many recommend using pattern matching to remove the option nature
of a value, although the valOf function used above is often more
convienient. Note the Option exception that valOf throws if fed
a NONE value, however.
--
Andrew Smallshaw
and...@sdf.lonestar.org