
-----BEGIN PGP SIGNED MESSAGE----- Hash: RIPEMD160 I'm toying around with web programming in Haskell. I'm trying to write a script which GETs an id and returns a couple of random numbers. Something like this: cgiMain :: CGI CGIResult cgiMain = do inp <- getInput "id" let gen = parse inp output $ take 10 (randoms gen) parse :: Maybe String-> StdGen parse (Just x) = read x parse Nothing = undefined Now I'd like to get a new StdGen, in case no id was supplied to the script. cgiMain :: CGI CGIResult cgiMain = do inp <- getInput "id" gen <- parse inp output $ take 10 (randoms gen) parse :: Maybe String-> IO StdGen parse (Just x) = return $ read x parse Nothing = getStdGen Obviously this doesn't work because I'm trying to do IO inside CGI (right?). Is there some incantation I can perform to make this possible? Like gen <- arcaneMagic parse inp As you probably have noticed I don't know very much about monads, all I did until now was reading or writing some files. Thanks in advance -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGzudU11V8mqIQMRsRA33RAJ9buZDHgz/eXi8Jw9OBwbTErDccRgCfbGrr 1WXiGHmxlTBe01E409yJyv8= =XSDj -----END PGP SIGNATURE-----

Adrian Neumann wrote:
Now I'd like to get a new StdGen, in case no id was supplied to the script.
parse :: Maybe String-> IO StdGen parse (Just x) = return $ read x parse Nothing = getStdGen
Obviously this doesn't work because I'm trying to do IO inside CGI (right?). Is there some incantation I can perform to make this possible?
Abracadabra, the incantation is liftIO :: IO a -> CGI a i.e. parse :: Maybe String-> CGI StdGen parse (Just x) = return $ read x parse Nothing = liftIO getStdGen Regards, apfelmus

2007/8/24, Adrian Neumann
Obviously this doesn't work because I'm trying to do IO inside CGI (right?). Is there some incantation I can perform to make this possible? Like
gen <- arcaneMagic parse inp
As doing IO in the CGI Monad is a current need, it's an instance of MonadIO and as such provide liftIO, so your arcaneMagic is called liftIO : cgiMain :: CGI CGIResult cgiMain = do inp <- getInput "id" gen <- liftIO $ parse inp output $ take 10 (randoms gen) parse :: Maybe String-> IO StdGen parse (Just x) = return $ read x parse Nothing = getStdGen -- Jedaï
participants (3)
-
Adrian Neumann
-
apfelmus
-
Chaddaï Fouché