functional programming - Standard ML: Basic Conversion to Uppercase Character -


i'm trying write function converts lower case character uppercase (if uppercase, leave unchanged).

here's i've written:

fun toupper(mychar) =     exception invalidcharacter;     if ord(mychar) >= ord(#"a") andalso ord(mychar) <= ord(#"z") mychar     else         if ord(mychar) >= ord(#"a") andalso ord(mychar) <= ord("z")             chr(ord(mychar) - (ord(#"a") - ord(#"a")));         else raise invalidcharacter; 

i'm getting compilation error:

ullman.sml:66.12 error: syntax error: inserting  equalop  uncaught exception compile [compile: "syntax error"]   raised at: ../compiler/parse/main/smlfile.sml:15.24-15.46              ../compiler/toplevel/interact/evalloop.sml:44.55              ../compiler/toplevel/interact/evalloop.sml:292.17-292.20 

am not allowed define exceptions within function i've done?

thanks help, bclayman

no, need define exception in let expression, such as:

fun toupper(mychar) =   let exception invalidcharacter;   in if ord(mychar) >= ord(#"a") andalso ord(mychar) <= ord(#"z") mychar      else       if ord(mychar) >= ord(#"a") andalso ord(mychar) <= ord(#"z")           chr(ord(mychar) - (ord(#"a") - ord(#"a")))       else raise invalidcharacter   end 

that said, better practice define exception outside of let expression, can caught user. example, if wanted string, 1 might do:

fun touppers(str) = string.implode (map toupper (string.explode str))  

there no way recover invalidcharacter exception, unless handle exceptions (which bad practice):

fun touppers(str) =     string.implode (map toupper (string.explode str)) handle e => (*do something*) 

instead like:

fun touppers(str) =     string.implode (map toupper (string.explode str)) handle invalidcharacter => (*do something*) 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -