0
votes

I need to convert a string type to real type in sml without using the function Real.fromString in sml.

For example: input "12.3"->output 12.3:real

2

2 Answers

1
votes

i donot want to use any external library in the conversation

This doesn't make sense. For starters the Real module is not a external library as such, but a required part of the basis library that every SML implementation ought to supply (see this link).

If you don't want to use any part of the basis library, then I would advise you not to do anything at all as it will leave you with almost nothing.

On the other hand if you want to implement this yourself for fun then you will have to parse the string and produce a real along the way. The basis library does this using a real scanner and StringCvt.scanString. The MLton implementation of the real scanner is about 245 lines not counting the auxiliary functions that help it on the way such as the Char module.

If this is what you want to do, then take a look at how they implement it. Else, just use the functions from the basis library, that is what they are there for.

0
votes

try Real.fromString, which has type string -> real option:

val SOME x = Real.fromString "12.3";