If you are steaming with compicated codes, then how about taking a break. Let's play with a simple cat. \begin{code} main = mapM (>>=putChar) getCharS where getCharS = getChar:getCharS \end{code} Tested with ghc. Works good except that you get some messages on stderror because eof is not handled. How would you suggest to neatly insert the error handling code into ? P.S. Instead of coding with C++, I want to write my server main code like this. server_main = mapM (>>=process.reply) where getReqS = getReq:getReqS Only if I had enough time ... :-p Using HDirect and so on ... -- Ahn Ki-yung
On Wed, Nov 13, 2002 at 03:29:53PM +0900, Ahn Ki-yung wrote:
If you are steaming with compicated codes, then how about taking a break. Let's play with a simple cat. \begin{code} main = mapM (>>=putChar) getCharS where getCharS = getChar:getCharS \end{code}
Why not this? main = mapM_ (\h -> mapM_ putChar =<< hGetContents h) =<< mapM (flip openFile $ ReadMode) =<< getArgs Bill
I'm not sure why someone hasn't suggested main = interact id which I think would accomplis everything you want, and probably be a heck of a lot faster, as (apparently) putChar and getChar are quire inefficient. -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Wed, 13 Nov 2002, William Lee Irwin III wrote:
On Wed, Nov 13, 2002 at 03:29:53PM +0900, Ahn Ki-yung wrote:
If you are steaming with compicated codes, then how about taking a break. Let's play with a simple cat. \begin{code} main = mapM (>>=putChar) getCharS where getCharS = getChar:getCharS \end{code}
Why not this?
main = mapM_ (\h -> mapM_ putChar =<< hGetContents h) =<< mapM (flip openFile $ ReadMode) =<< getArgs
Bill _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
On Wed, 13 Nov 2002, William Lee Irwin III wrote:
main = mapM_ (\h -> mapM_ putChar =<< hGetContents h) =<< mapM (flip openFile $ ReadMode) =<< getArgs
On Wed, Nov 13, 2002 at 07:46:41AM -0800, Hal Daume III wrote:
main = interact id
There is a semantic difference here, as the version I posted above takes files from the command-line, though it does fail to accommodate the pass-through case, which is handled by: main = getArgs >>= \args ->if args == [] then interact id else mapM readFile args >>= mapM_ putStr .. which seems to be a bit above 80 chars. Some library function trickery is probably in order to cut the if statement down to size. e.g. nonEmptyMapM_ :: Monad m => m () -> (t -> m ()) -> [t] -> m () nonEmptyMapM_ def _ [] = def nonEmptyMapM_ _ f xs@(_:_) = mapM_ f xs main = getArgs >>= nonEmptyMapM_ (interact id) ((>>= putStr) . readFile) Bill
On Wed, 2002-11-13 at 22:43, William Lee Irwin III wrote:
There is a semantic difference here, as the version I posted above takes files from the command-line, though it does fail to accommodate the pass-through case, which is handled by: [...]
I need this behavior often enough to justify writing a small module to provide it: Full version at http://tea.moertel.com/~thor/ravt/ravt-0.9/GetInput.hs -- GetInput.hs -- Tom Moertel <tom@moertel.com> -- CVS $Id: GetInput.hs,v 1.1 2002/09/09 04:57:23 thor Exp $ -- | This module provides a method of getting input from files named -- on the command line or, if no files are provided, from standard -- input. This mimics Perl's default input handling, which is -- convenient. Also, the module provides versions of the standard -- 'interact' function that use these input-getting behaviors. module GetInput ( getInputDefault, getInputFromArgs , interactDefault, interactFromArgs) where import Control.Monad (liftM) import System.Environment (getArgs) -- | Reads the arguments passed on the command line and then passes -- them to 'getInputFromArgs' for handling. getInputDefault :: IO String getInputDefault = getArgs >>= getInputFromArgs -- | Treats the input list as a list of files from which to read -- input sequentially. If the list is empty, input is read from -- standard input. If "-" is passed as a file, it is taken to -- mean standard input. getInputFromArgs :: [String] -> IO String getInputFromArgs [] = getContents getInputFromArgs xs = liftM concat (mapM readFromFile xs) where readFromFile "-" = getContents readFromFile file = readFile file -- | Gets input via 'getInputDefault', processes it with the -- function argument @f@, and then prints the @String@ that -- @f@ returns. interactDefault :: (String -> String) -> IO () interactDefault f = getInputDefault >>= putStrLn . f -- | Gets input via 'getInputFromArgs', processes it with the -- function argument @f@, and then prints the @String@ that -- @f@ returns. interactFromArgs :: [String] -> (String -> String) -> IO () interactFromArgs args f = getInputFromArgs args >>= putStrLn . f
On Wed, 2002-11-13 at 22:43, William Lee Irwin III wrote:
There is a semantic difference here, as the version I posted above takes files from the command-line, though it does fail to accommodate the pass-through case, which is handled by: [...]
On Thu, Nov 14, 2002 at 05:33:59PM -0500, Tom Moertel wrote:
I need this behavior often enough to justify writing a small module to provide it: Full version at http://tea.moertel.com/~thor/ravt/ravt-0.9/GetInput.hs getInputFromArgs :: [String] -> IO String getInputFromArgs [] = getContents getInputFromArgs xs = liftM concat (mapM readFromFile xs)
This looks very useful, though less general than nonEmptyMapM_ I'll likely be using myself. Thanks, Bill
participants (4)
-
Ahn Ki-yung -
Hal Daume III -
Tom Moertel -
William Lee Irwin III