如何在Haskell中打印每个单词长度的一组单词

如何在Haskell中打印每个单词长度的一组单词,haskell,input,split,Haskell,Input,Split,我试着用Haskell打印一组单词和每个单词的长度。这些单词在.txt文件中,因此首先我必须将内容放入Haskell,然后我将它们拆分为单个单词,因此到目前为止我已经这样做了: main = do textdat <- openFile "palindrom.txt" ReadMode inhalt <- hGetContents textdat winhalt <- return (words inha

我试着用Haskell打印一组单词和每个单词的长度。这些单词在.txt文件中,因此首先我必须将内容放入Haskell,然后我将它们拆分为单个单词,因此到目前为止我已经这样做了:

main =
       do
          textdat <- openFile "palindrom.txt" ReadMode
          inhalt <- hGetContents textdat
          winhalt <- return (words inhalt)
          print winhalt
          putStrLn "has the length "
          print (length winhalt)
(45是.txt文件中的字数)

如何将单词序列拆分为单个单词,以便分别获取和打印每个单词的长度?

如果您运行此代码(在其自己的源代码上):

main=do
目录
"wordexample1""wordexample2""wordexample3"..."wordexample45"
has the length
45
main = do                                                                                                                                                                                               
    contents <- readFile "words.hs"
    print $ [(w, length w) | w <- words contents]
[("main",4),("=",1),("do",2),("contents",8),("<-",2),("readFile",8),("\"words.hs\"",10),("print",5),("$",1),("[(w,",4),("length",6),("w)",2),("|",1),("w",1),("<-",2),("words",5),("contents]",9)]
main = do  
    contents <- readFile "words.hs"
    putStr $ unlines [w ++ ": " ++ show (length w) | w <- words contents]