haskell - MaybeT/Maybe and IO: Failsafe reading of information -
i trying read information entered user , parse type person
, uses type gender
. so, use code:
data person = person string int gender string data gender = male | female | notspecified deriving read instance show gender show male = "male" show female = "female" show notspecified = "not specified" instance show person show (person n g j) = "person {name: " ++ n ++ ", age: " ++ show ++ ", gender: " ++ show g ++ ", job: " ++ j ++ "}" readpersonmaybet :: maybet io () readpersonmaybet = putstrln "name?:" name <- getline putstrln "age?:" agestr <- getline putstrln "gender?:" genderstr <- getline putstrln "job?:" job <- getline let newperson = person name (read agestr) (read genderstr) job putstrln $ show newperson
now make more failsafe - achieve tried use maybet monad. using this, got code:
readpersonmaybet :: maybet io () readpersonmaybet = lift $ putstrln "name?:" name <- lift getline lift $ putstrln "age?:" agestr <- lift getline lift $ putstrln "gender?:" genderstr <- lift getline lift $ putstrln "job?:" job <- lift getline let newperson = person name (read agestr) (read genderstr) job lift $ putstrln "show newperson"
it compiles/loaded ghci, when try execute readpersonmaybet
function error-message
no instance (data.functor.classes.show1 io) arising use of `print' in stmt of interactive ghci command: print it
how can solve issue? writing code, used wikibook monad transformers.
edit: when try 'run' runmaybet
gets executed, not failsafe @ all. entering nonsense age example still results in output like
person {name: 85, age: *** exception: prelude.read: no parse.
runmaybet
? – pdexter aug 8 '16 @ 14:04person
– erikr aug 8 '16 @ 14:12readmaybe
– pdexter aug 8 '16 @ 14:13do
block. you're never doing in actualmaybe
monad. – pdexter aug 8 '16 @ 14:23