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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!postnews1.google.com!not-for-mail From: andreatta@mail.chem.sc.edu (Dan Andreatta) Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: 24 Sep 2004 10:40:56 -0700 Organization: http://groups.google.com Message-ID: <338040f8.0409240940.4dd90c93@posting.google.com> References: <338040f8.0409230912.70e3375b@posting.google.com> NNTP-Posting-Host: 129.252.151.81 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1096047657 6053 127.0.0.1 (24 Sep 2004 17:40:57 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 24 Sep 2004 17:40:57 +0000 (UTC) Xref: g2news1.google.com comp.lang.ada:4118 Date: 2004-09-24T10:40:56-07:00 List-Id: Pascal Obry wrote in message news:... > 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! > > Pascal. I understand. Functional languages require a change in prospective. But also I made unreadable to have a less lines :-). Hope now you find it more readable. Here is slightly rewritten: module Main where import Data.FiniteMap import Data.List import System import Char count_words :: [String] -> [(String, Integer)] count_words ws = wc where comp (_,n) (_,m) = compare m n -- used in sortBy to sort -- the values, not keys ws' = zip ws [1,1..] -- creates a list [("word",1),("word",1)...] -- In functional languages the hash table is substituted with a -- finite map -- emptyFM creates an empty finite map -- addToFM adds an element in the form (key, value) to a -- finite map. If the element already exists, -- it is replaced by the new one. -- addListToFM adds a list of elements, -- [(key, value), (key, value)...] -- addListToFM_C instead applies an operation (e.g. +) between -- the values of the old and new elements with the same key. -- Since every key has value 1, -- we count the number of keys in the list. fm = addListToFM_C (+) emptyFM ws' list = fmToList fm -- Creates a list from the finite map in the -- form [(key,value)], with the keys ordered. wc = sortBy comp list -- sort the list in ascending order -- of the values. -- this to take care of alphanumeric symbols. -- Replace them with spaces. removeSymbol x = if isAlphaNum x then x else ' ' main = do args <- getArgs -- get the command line arguments as -- a list of strings. f <- readFile (args!!0) -- the first one is the one we want. -- And read the whole file and -- return a String let f1 = map (removeSymbol.toLower) f -- convert to lowercase and substitue spaces -- for symbols. For every character, convert -- it to lowercase and remove it if a symbol let f2 = words f1 -- divide the string in a list of strings let wc = count_words f2 let wc10 = take 10 wc -- take only the first ten elements putStrLn ( show wc10 ) -- and print them