sml - SMLNJ if statement error -


i'm trying learn sml , wrote small game resembling 1 in zed shaw's book 'learn ruby hard way'. code below works fine in repl , compiles mlton. however, results in else statement ("error!"). if try debug print(valof(response)) in place of if statements, returns whatever string type (an empty string if don't type anything). don't think inputline function, not let statement not return stdin, , should not comparison op.

fun first_room () =     (print "you're in dark, mottled room.\n";      print "which 1 choose? left or right?\n";      print "> ";      let val response = (textio.inputline textio.stdin)      in      if response = none      print "you stay.\n"      else if valof(response) = "left"      print "you go left.\n"      else if valof(response) = "right"      print "you go right.\n"      else print "error!\n"      end)  fun main () = first_room ()  val _ = main () 

so, have 2 questions: 1 - may cause this? 2 - ml (functional) way build procedural program?

following sebastian's answer:

just inform casual reader:

looking ways debug error, noticed if type textio.inputline textio.stdin; in repl, , type abc, "abc\n" back. didn't knew 1 can use repl trying ml programs without compiling them.

what may cause this?

the value returned textio.inputline includes trailing newline. is, if write left , press enter, string "left\n" returned value.

to take care of this, can make simple trimming function, can use remove newline.

(* trimlast : string -> string    trims off last character in string *) fun trimlast s = string.substring(s, 0, size s - 1) 

is ml (functional) way build procedural program?

instead of writing chain of if-expressions, lot cleaner if made use of pattern matching, separated out handling of option type returned inputline. write this:

(* trimlast : string -> string  * trims off last character in string *)     fun trimlast s = string.substring(s, 0, size s - 1)  fun processinput f =     case textio.inputline textio.stdin of         none   => print "goodbye.\n"       | s => f (trimlast s)  (* empty room functions, make program compile *) fun leftroom () = () fun rightroom () = ()  fun firstroom () = (     print "you're in dark, mottled room.\n";     print "which 1 choose? left or right?\n";     print "> ";     processinput (fn         "left"   => ( print "you go left.\n"; leftroom () )       | "right"  => ( print "you go right.\n"; rightroom () )       | _        => ( print "you nothing.\n"; firstroom () )     )   ) 

notice how i've made function processinput, takes care of reading input, , handling option values. allows to, instance, normalize input in 1 place, instance if wanted lowercase it, handle both "left" , "left".

furthermore, specify rules handling input function, pattern matches on string given.

i consider style more functional , clean.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -