【问题标题】:word count utility in Haskell [closed]Haskell中的字数统计实用程序[关闭]
【发布时间】:2015-02-04 16:01:52
【问题描述】:

我对 Haskell 还很陌生。几天来,我一直在尝试创建一个实用程序来计算 Haskell 中的单词和行数,以帮助我更好地理解该语言。但是,我正在努力让它发挥作用。

到目前为止,我有:

wordCountUtility = do

       putStrLn "Please enter the filename:"

       filename <- getLine

       putStrLn ("The file name you have entered is: " ++ filename)

      contents <- readFile filename -- read the file specified in “name” into “contents”

      lower <- (return . map toLower) contents

      putStrLn lower

我尝试使用 'chop' 并找到 print . length . words =&lt;&lt; getContents 并对其进行了多次修改,但我没有运气。 我也在 Stack Overflow 上看过不少类似的答案,例如:identifying number of words in a paragraph using haskell

输出应该有点类似这样:

Amount of Lines within the file
Lines : 10

Amount of Words found within the file
Words : 110

任何帮助将不胜感激。

【问题讨论】:

  • 那么您的问题到底是什么?您发布的代码在 SO 上的格式不正确,我首先建议您修复它,但您几乎只是描述了您想要做什么,而不是您遇到的问题。
  • 对不起,我刚接触堆栈溢出,它没有正确复制代码。我在使用 count 函数时遇到问题,因为我想返回文件中的行数和字数。我现在已经能够解决这个问题了。
  • 我遇到的新问题是 ReadFile 没有返回正确的行数。例如,如果我给它一个有 4 行的文件“new.txt” - 它返回 4 但是当我通过控制台输入它时它返回一个 :( 有什么帮助吗?谢谢
  • 更新 - 也设法解决了这个问题。是对齐问题。还是谢谢!
  • 您还没有发布任何实际计算行数或字数的代码。您确定您的问题中有相关代码吗?此外,您可以编辑您的帖子以使用更多信息更新它或修复格式,我鼓励您将任何相关信息放在帖子本身而不是 cmets 中。评论更多是为了澄清,应该被认为是暂时的,所有相关信息都应该出现在问题中,以便其他人更容易找到它。

标签: haskell count lines word-count


【解决方案1】:

您的wordCountUtility 可能还没有计算字数。你应该停在像

commandLineUtility :: (String -> String) -> IO ()
commandLineUtility fn = do
       putStrLn "Please enter the filename:"
       filename <- getLine
       putStrLn ("The file name you have entered is: " ++ filename)
       contents <- readFile filename -- read the file specified in “name” into “contents”
       lower <- (return . fn) contents
       putStrLn lower 

(我尽可能地贴近你的文字。)但是,现在,你必须弄清楚什么 (String -&gt; String) 要应用的函数。这是一个纯函数,应该单独开发。所以你可以写:

cwlcount :: String -> (Int, Int, Int)
cwlcount str = (length str, length (words str), length (lines str))

format :: (Int, Int, Int) -> String
format (c,w,l) = unlines $
     ["Number of characters:"
     , show c
     , "Number of words:"
     , show w
     , "Number of lines:"
     , show l
     ]

所以(format . cwlcount) :: String -&gt; String,你可以写:

 main :: IO ()
 main = commandLineUtility (format . cwlcount)

当然,对这个程序有上百万的反对意见,但您可以通过逐个调查部分来改进它。一方面,将整个字符列表带入内存并分别对其进行三个长度计算是令人恼火的。 Predude.getLine 也不是很友好......

那么,目前我们的结果是这样的:

$ ghci Sonia_CS.hs 
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( Sonia_CS.hs, interpreted )
Ok, modules loaded: Main.
>>> main
Please enter the filename:
Sonia_CS.hs
The file name you have entered is: Sonia_CS.hs
Number of characters:
816
Number of words:
110
Number of lines:
25

或者更好:

$ ghc -O2 Sonia_CS.hs 
[1 of 1] Compiling Main             ( Sonia_CS.hs, Sonia_CS.o )
Linking Sonia_CS ...
$ ./Sonia_CS 
Please enter the filename:
/usr/share/dict/words
The file name you have entered is: /usr/share/dict/words
Number of characters:
2493109
Number of words:
235886
Number of lines:
235886

【讨论】:

  • 非常感谢亚瑟。我设法提出了一个解决方案,现在使用这种格式 str = (show (length (lines str))) 使用 countLines 和 countWords,这似乎也可以正常工作。
  • 对,我将结果的格式与它们的收集分开,因为我将 IO 与纯计算分开。当然,它们都可以放在一起。
  • 是的,我更喜欢你的方法,因为它看起来更简洁!谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2010-11-04
  • 2011-03-26
  • 1970-01-01
  • 2014-10-01
  • 2012-04-19
相关资源
最近更新 更多