« Return to Thread: Network.CGI -- practical web programming example.

Re: Network.CGI -- practical web programming example.

by Brandon S. Allbery KF8NH :: Rate this Message:

Reply to Author | View in Thread

On Jun 27, 2009, at 20:07 , Edward Ing wrote:

> saveFile n =
>    do          cont <- (liftM fromJust) $ getInputFPS "file"
>         let f = uploadDir ++ "/" ++ basename n
>         liftIO $ BS.writeFile f cont
>         return $ paragraph << ("Saved as " +++ anchor ! [href f] <<  
> f +++ ".")
>
> saveFile n =
> do   cont <- getInputFPS "file"
>        let f = uploadDir ++ "/" ++ basename n
>       liftIO $ BS.writeFile f (fromJust cont)
>       return $ paragraph << ("Saved as " +++ anchor ! [href f] << f +
> ++ ".")
>
> 1) Why did the author choose to insert "liftM" in function saveFile?

It's because of where fromJust is being called.  In yours, it's being  
used at a place that expects a normal value, so you can just go ahead  
and use it.

The original is applying the fromJust inside of a monadic computation,  
as indicated by the (<-), so it needs to be lifted.  Some Haskell  
programmers use fmap (because most Monads are also Functors), others  
use liftM.  Both have the same effect:  given a monadic computation "m  
a", "liftM f" turns "f" into a function that operates on the enclosed  
"a" instead of the entire "m a".

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@...
system administrator [openafs,heimdal,too many hats] allbery@...
electrical and computer engineering, carnegie mellon university    KF8NH




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

PGP.sig (202 bytes) Download Attachment

 « Return to Thread: Network.CGI -- practical web programming example.