Log in

View Full Version : Haskell-word count



qzpmqzpm
10-16-2009, 11:25 PM
1) Script Title: Haskell-word count

2) Script URL (on DD):



3) Describe problem:


For example: when count: putStr(count "The world the Spain") it should produce:

the.............................2
world...........................1
spain...........................1 thats the way I want


but in this code, it produces:

the....................... **
world.................... *
spain......................*

I dont want this way.


and the below is the following codes:

-- Word frequency count.
module Count where

import List
import Char






-- Convert an input text to a text representation of a count of
-- frequencies of words in the original text.
--
-- There is a line of output for each distinct word of the input,
-- ignoring punctuation and case, consisting of
-- * the word (padded on the left so that the right ends of the words
-- are vertically aligned,
-- * a space followed by as many asterisks as there were occurrences
-- of the word in the input.
-- The words are listed in alphabetical order.

count :: String -> String
count s =
unlines [count_line (pad width (head ws)) (length ws) | ws <- groups]
where
-- list of groups of identical words in the input
groups = group(sort [normalize w | w <- words s])

-- width of the longest word in the input
width = maximum[length (head ws) | ws <- groups]

-- a line of the count for word w with n occurrences
count_line :: String -> Int -> String
count_line w n = w ++ "" ++ replicate 48 '.'


-- The string consisting of letters and hyphens from the input string,
-- converted to lower case.
normalize :: String -> String
normalize cs = [toLower c | c <- cs, isAlpha c || c == '-']


-- pad s n is a string formed by adding enough spaces in front of s to
-- make a string of length at least n.
pad :: Int -> String -> String
pad n s = replicate (1 - length s) ' ' ++ s


please help

Trinithis
11-04-2009, 10:16 PM
http://www.dynamicdrive.com/forums/showthread.php?t=37934&highlight=haskell+count