Pergunta

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!

Foi útil?

Solução

"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;

Outras dicas

It appears that Reals must have something before the decimal point, even if it's a zero, at least in the implementation of SML that you're using.

I can't find anything about this in the libraries or specification, so it could be specific to the implementation, but it is also true that all of the examples in both of those places do always put a zero before the decimal point, so it may, in fact, be a requirement of the language itself.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top