如何将列表转换为字符串?

时间:2017-10-14 18:44:00

标签: haskell

我创建一个读取文件的函数,并在每一行中删除之前在同一行中遇到的所有单词。

{-# OPTIONS_GHC -Wall #-}
module Main where
import System.Environment
import System.IO()

main :: IO ()
main = do args <- getArgs 
          if (length args > 0) then do
            f <- get args
            putStrLn (seqWord $ head f)
          else do
            f <- getContents
            putStrLn (seqWord f)

get :: [String] -> IO[String]
get [] = return []
get (file:xs) = do
  contents <- readFile file
  fs <- get xs
  return (contents:fs)

seqWord :: String -> String
seqWord s = show (map (filterWord . words) (lines s))

filterWord :: [String] -> [String]
filterWord [] = []
filterWord (x:xs) = x : filterWord (filter(/=x) xs)

在回答中我有列表列表,比如这个

[["1","12","5","8","13","145","85"],["546","822","1","12","58","8","9"]]

请帮我解决这个问题。谢谢

1 个答案:

答案 0 :(得分:1)

使用unwords功能撤消words的效果。您可能还想将show替换为unlines

seqWord s = unlines (map (unwords . filterWord . words) (lines s))