
Paul Moore wrote:
On 25 Dec 2005 12:24:38 +0100, Peter Simons
wrote: Paul Moore writes:
It would be interesting to see standalone code for wcIOB (where you're allowed to assume that any helpers you need, like your block IO library, are available from the standard library). This would help in comparing the "obviousness" of the two approaches.
A simple version of the program -- which doesn't need any 3rd party modules to compile -- is attached below. My guess is that this approach to I/O is quite obvious, too, if you have experience with system programming in C.
Hmm, I can't honestly believe that you feel that your code is as "obvious" as the original. I'm not unfamiliar with monads and state, and I know C well, but it took me a significant amount of time to decipher your code (even knowing what it was intended to do), whereas I knew what the original was doing instantly.
IMHO, the main point of the example in the article is that
wc :: String -> (Int, Int, Int) wc file = ( length (lines file) , length (words file) , length file )
is a crapy word-counting algorithm.
Dunno. It's certainly not a bad (executable!) definition of the problem. My point is that Haskell allows me to write *very* clear "executable pseudocode", but that code is not a good starting point for writing production-quality code.
this program counts two times length of lists of strings formed by lines, and words and third time counts again length of file. This is not just word counting program, it creates two additional lists, which are not used anywhere but to count :) While it is certainly expressive, in terms of programing is pointless. No one would write such a code for word counting. Here is what I would write in Haskell, same logic as in C++ (i don;t know standard lib ): module Main where import IO import Char main = do s <- hGetContents stdin putStrLn $ show $ wc s wc :: String -> (Int , Int , Int) wc strs = wc' strs (0,0,0) where wc' [] res = res wc' (s:str) (lns, wrds, lngth ) | s == '\n' = wc' str (lns+1,wrds, lngth+1) | isAlpha s = wc'' str (lns, wrds+1,lngth+1) | otherwise = wc' str (lns,wrds, lngth+1) wc'' [] res = res wc'' (s:str) (lns,wrds,lngth) = if isAlphaNum s then wc'' str (lns,wrds,lngth+1) else wc' str (lns,wrds, lngth+1) Greetings, Bane.

From: Branimir Maksimovic
module Main where import IO import Char
main = do s <- hGetContents stdin putStrLn $ show $ wc s
wc :: String -> (Int , Int , Int) wc strs = wc' strs (0,0,0) where wc' [] res = res wc' (s:str) (lns, wrds, lngth ) | s == '\n' = wc' str (lns+1,wrds, lngth+1) | isAlpha s = wc'' str (lns, wrds+1,lngth+1) | otherwise = wc' str (lns,wrds, lngth+1) wc'' [] res = res wc'' (s:str) (lns,wrds,lngth) = if isAlphaNum s then wc'' str (lns,wrds,lngth+1) else wc' str (lns,wrds, lngth+1)
err, I've tested windows file on unix :) wc'' strs@(s:str) (lns,wrds,lngth) = if isAlphaNum s then wc'' str (lns,wrds,lngth+1) else wc' strs (lns,wrds, lngth)
Greetings, Bane.
_________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

On 12/26/05, Branimir Maksimovic
Paul Moore wrote:
Dunno. It's certainly not a bad (executable!) definition of the problem. My point is that Haskell allows me to write *very* clear "executable pseudocode", but that code is not a good starting point for writing production-quality code.
this program counts two times length of lists of strings formed by lines, and words and third time counts again length of file. This is not just word counting program, it creates two additional lists, which are not used anywhere but to count :) While it is certainly expressive, in terms of programing is pointless. No one would write such a code for word counting.
Here is what I would write in Haskell, same logic as in C++ (i don;t know standard lib ):
Yes, that is a very reasonable way of improving performance. The original algorithm, as you say, does 3 passes through the file, and this does one, computing the 3 values "in parallel". So it does seem a sensible next step. I must try timings to see how much this improves the timings, and how much further there is to go :-) Thanks, Paul.

Paul Moore wrote:
On 12/26/05, Branimir Maksimovic
wrote: Paul Moore wrote:
Dunno. It's certainly not a bad (executable!) definition of the problem. My point is that Haskell allows me to write *very* clear "executable pseudocode", but that code is not a good starting point for writing production-quality code.
this program counts two times length of lists of strings formed by lines, and words and third time counts again length of file. This is not just word counting program, it creates two additional lists, which are not used anywhere but to count :) While it is certainly expressive, in terms of programing is pointless. No one would write such a code for word counting.
Here is what I would write in Haskell, same logic as in C++ (i don;t know standard lib ):
Yes, that is a very reasonable way of improving performance. The original algorithm, as you say, does 3 passes through the file, and this does one, computing the 3 values "in parallel". So it does seem a sensible next step. I must try timings to see how much this improves the timings, and how much further there is to go :-)
Thanks, Paul. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Dec 26, 2005 at 09:10:56PM +0100, Branimir Maksimovic wrote:
this program counts two times length of lists of strings formed by lines, and words and third time counts again length of file. This is not just word counting program, it creates two additional lists, which are not used anywhere but to count :) While it is certainly expressive, in terms of programing is pointless. No one would write such a code for word counting.
Come on! Sometimes the only thing you want is a simple, *correct* solution for the problem, for example just to test the ugly, efficient version. Best regards Tomasz -- I am searching for a programmer who is good at least in some of [Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland
participants (4)
-
Branimir Maksimovic
-
Chris Kuklewicz
-
Paul Moore
-
Tomasz Zielonka