Search code examples
smlsmlnjml

In SML, can you convert ".3" to the real "0.3"?


I'm pretty new to SML and I've been using SML/NJ. Let's say I have the following simple function:

fun test(x) = x / 2.0;

test(0.3); returns 0.15.

I'd like for it to also work with test(.3); Right now I'm getting the following error:

- test(.3);
stdIn:23.6-23.9 Error: syntax error: deleting  DOT INT RPAREN

Of course, I'd like it to work with any real of the form 0.X. Is this doable? Thank you!


Solution

  • "A real constant is an integer constant, possibly followed by a point (.) and one or more digits, possibly followed by an exponent symbol E and an integer constant; at least one of the optional parts must occur, hence no integer constant is a real constant. Examples: 0.7, +3.32E5, 3E~7 . Non-examples: 23, .3, 4.E5, 1E2.0 ."

    from: Definition of Standard ML Version 2 [Robert Harper, Robin Milner, Mads Tofte] 1988

    Update:

    The Definition of Standard ML (Revised) 1997 modifies the passage to:

    an exponent symbol (E or e ) and an integer constant in decimal notation;