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 contreturn $ 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] [email protected] system administrator [openafs,heimdal,too many hats] [email protected] electrical and computer engineering, carnegie mellon university KF8NH
PGP.sig
Description: This is a digitally signed message part
_______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
