[Haskell-cafe] Network.CGI -- practical web programming example.

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Jun 27 20:34:32 EDT 2009


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 at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090627/5e69df3d/PGP.bin


More information about the Haskell-Cafe mailing list