查找和替换文本文件输出中的单词

esp*_*all 1 haskell

我对haskell失败了,我在尝试让这个脚本工作时遇到了很多麻烦.目的是让它从命令行读取参数并在单独的文本文件中找到它们.然后将这些单词替换为与单词长度相同数量的星号.我设法让它找到替换字,但从那以后就撞墙了.我尝试了各种各样的,并希望有人能够为我澄清它.

module Main where

import System
import Data.Char

lowercase = map toLower


main = do (arg1:arg2:arg3:arg4:arg5:_) <- getArgs
          txt <- getContents
          putStr ( redact txt arg1 arg2 arg3 arg4 arg5 )


redact file w1 w2 w3 w4 w5 = unlines [ process line | line <- lines file ]
       where process line = unwords [ f word | word <- words line ]
             f w | lowercase(w) == lowercase(w1)   = convertWord w 1
                 | lowercase(w) == lowercase(w2)   = convertWord w 1
                 | lowercase(w) == lowercase(w3)   = convertWord w 1
                 | lowercase(w) == lowercase(w4)   = convertWord w 1
                 | lowercase(w) == lowercase(w5)   = convertWord w 1
                 | otherwise = w

convertWord :: Eq a => [a] -> [a] -> [a] -> [a]
convertWord [] _ = []

convertWord word count = temp
       where if count == 1 then let temp = ""
             if count <= length( word )
                        then temp = temp ++ "*"
                             convertWord word count+1
Run Code Online (Sandbox Code Playgroud)

我们的想法是调用convertWord部分并创建一个星号字符串,以反馈到要在输出中显示的redact.但是,当我尝试编译它时,GHC返回错误"redact.hs:28:13:解析错误(可能是错误的缩进)"

提前感谢您提供的任何帮助

汤姆

Tar*_*sch 6

你想要一个带字符串的函数"hello",然后把它变成"*****"(都有长度为5),这是正确的吗?

简单地使用map (const '*')(这是一个函数:)).示例:map (const '*') "hello"收益率"*****"

还有另一个变量,输入参数必须是[Char],而不是[a].为此用途map (asTypeOf '*').但是你可能不需要这个.

我真的不确定你想要什么.也许您可以澄清一下您的示例,举例说明您希望函数执行的操作.

哦,你的编译错误是由于你where以一种奇怪的方式使用语法.:)

希望这可以帮助!