Haskell 从文件中读取行并打印修改后的行

Haskell Reading lines from File and print these with modifications

我想读取一个每行包含一个单词的文件并逐行打印此文件,在行尾添加单词的长度。

到目前为止,我得到了这个解决方案,它删除了类型不匹配的错误。

我已经试了好几天了,但我完全失去了信心。我无法绕过 Haskell.

希望有人能帮助我。

这是我的代码:

import System.IO


main :: IO ()
main = do

  file <- readFile "palindrom.txt"
  putStrLn (unlines $ showLength $ lines file)

showLength (x:xs) = (x concat "has length of" length x) (showLength xs)

改为

showLength :: [[Char]] -> [[Char]]
showLength (x:xs) = -- (x concat "has length of" length x) (showLength xs)
            concat [x, " has length of ", show (length x)] : showLength xs
showLength  []  =  []

它会起作用的。您的原始代码在 Haskell.

中没有多大意义

concat :: [[a]] -> [a] 是一个内置函数,它将给定列表中的所有列表连接在一起:

> concat [[1],[2,3],[4]]
[1,2,3,4]

> concat ["a"," b ", "123"]
"a b 123"