2 Replies - 1204 Views - Last Post: 19 July 2014 - 05:30 PM

#1 rock123star   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-September 13

haskell prog

Posted 13 May 2014 - 02:28 PM

i am new to haskell language.
i have a programming assignment.

write a Haskell function that takes a text file as input, divides it into words, and counts the number of words of three letters or less. Then take the words and store them in a hash table, a data structure that is built into Haskell.
I want to know if this is the right way.
--word count
type WordCount = (Int)

--count file
countFile :: FilePath -> IO WordCount
countFile file = do
    text <- readFile file
    return $ getCount text

--get count
getCount :: String -> WordCount
getCount = foldl' (\v w x -> (w+length<3 (words x)))
                  . lines
-- print count
printCountLine :: Opts -> FilePath -> WordCount -> IO ()
printCountLine opts f (ws) =
    putStrLn ("\t" ++ (if showWords opts then (show ws) ++ "\t" else "")
                   ++ f)

Is This A Good Question/Topic? 0
  • +

Replies To: haskell prog

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2616
  • View blog
  • Posts: 4,173
  • Joined: 21-June 11

Re: haskell prog

Posted 13 May 2014 - 02:46 PM

No, it's not right. If you try to compile the code, you'll see that you'll get a compilation error.

Particularly w+length<3 (words x) is wrong because 3 (words x) is not valid - it tries to apply the function 3 to the arguments words x, which is wrong because 3 is not actually a function (unless you have some very weird Num instances somewhere in your code).

Similarly length is a function, not a number, so w + length doesn't make sense either.

Another problem is that the function given to foldl' needs to take 2 arguments, not 3.

It is also not clear to me why you use lines when the assignment doesn't talk about lines at all - only words.
Was This Post Helpful? 1
  • +
  • -

#3 gonzaw   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 46
  • Joined: 18-December 12

Re: haskell prog

Posted 19 July 2014 - 05:30 PM

Yeah, it seems you made the getCount function over complicated

Try going through it by thinking about what you really want from the function.

"I want getCount to get a string which represents the file, divide it into words, then count only the words that have 3 letters or less".

So, you can start thinking that you first divide it into words, then you take the result, which would be a list of words, and count only the ones that have 3 letters or less. You could do this last one by filtering the list, taking only the ones that have 3 letters or less, and then counting it.

So you could end up with:

getCount :: String -> WordCount
getCount = length . filter ((<4) . length) . words

That solves it. Maybe it could perform better, but you can add optimizations for it later if it affects your program enough that it's needed to (in Real World Haskell there are some tips on how to optimize it).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1