Converting a string to int in SML -


in attempt create interpreter simple language in sml, struggling convert string integer. instance,

val somestring = " 1.9" int.fromstring somestring 

returns:

val 1 : int option 

furthermore, when try extract value option type using:

valof(int.fromstring somestring); 

it returns:

val = 1 : int

i confused why still converting string integer though real number. , how can convert string int , handle errors if any.

i don't know how conversion in every implementation works, in poly/ml valid int returned if valid int has been read far - if whole string not valid int.

as molbdnilo pointed out, easier use char.isdigit function (or own allows more int cases) check if string int:

list.all char.isdigit (string.explode somestring) 

concerning confusion valof, expected because saw int.fromstring returned int option. valof function defined this:

fun valof (opt: 'a option) : 'a =     case opt of         none => raise fail "option none"       | => 

so since saw int.fromstring " 1.9" returned some 1, must case valof (int.fromstring " 1.9") returns 1.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -