
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

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