From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,243dc2fb696a49cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!newsfeed.stueberl.de!news-FFM2.ecrc.net!News-Peer-Europe!news.stupi.se!newsfeed.sunet.se!news01.sunet.se!dd.chalmers.se!legolas.gidenstam.org!nobody From: anders@use.reply.to (Anders Gidenstam) Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: Fri, 24 Sep 2004 00:08:19 +0200 Organization: Chalmers University of Technology, Sweden Message-ID: References: <338040f8.0409230912.70e3375b@posting.google.com> Reply-To: anders-www@gidenstam.org (Anders Gidenstam) NNTP-Posting-Host: zsh.cs.chalmers.se Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Newsreader: knews 1.0b.1 Xref: g2news1.google.com comp.lang.ada:4053 Date: 2004-09-24T00:08:19+02:00 List-Id: In article , Pascal Obry writes: > > andreatta@mail.chem.sc.edu (Dan Andreatta) writes: > >> And I write it in Haskell. 14 lines. OK, I cheated, I used GHC libraries. > > And it is just unreadable ! I just can't understand how this is supposed > to work! It is not all that horrible once you realize that $ is function application, for example putStr $ show $ take 10 $ count_words (map (map toLower) $ words f) can also be written putStr (show (take 10 (count_words (map (map toLower) (words f))))) Still, I'm not sure Dan's version behaves as expected since it sometimes treats non-alphabetic characters as words. Here is another Haskell version and this time in pure Haskell 98 at the expense of one more line. Enjoy! module Main where import System import List import Char count_words :: [String] -> [(Int, String)] count_words xs = sortBy comp $ init $ foldr count_same [(0, "")] $ sort xs where count_same w1 (y1@(c, w2):ys) = if (w1 == w2) then (c + 1, w1):ys else (1,w1):y1:ys comp (c1, _) (c2, _) = compare c2 c1 main = do args <- getArgs f <- readFile (args!!0) putStr $ concat $ map prettyShow $ take 10 $ count_words $ wordify f where wordify xs = words $ map toLower $ map onlyAlpha xs onlyAlpha x = if isAlpha x then x else ' ' prettyShow (c,w) = w ++ " " ++ show c ++ "\n" Well, I think it is nifty even if you don't :) /Anders -- "Luck is my middle name," said Rincewind, indistinctly. "Mind you, my first name is Bad." -- (Terry Pratchett, Interesting Times)