从文件中删除数字

时间:2017-10-31 12:31:35

标签: haskell

我正在尝试创建没有字符串数字的新文件

main :: IO ()
main = do
    contents <- readFile "input1.txt"
    putStr (process contents)

check = if isDigit x
            x = "a"

process :: String -> String         
process = map check

但是得到了这个错误:“表达式中的语法错误(意外符号”进程“)”“。我做错了什么?

1 个答案:

答案 0 :(得分:2)

在Haskell中,if“语句”实际上是表达式,必须返回一个值。所以你需要有一个else块。

import Data.Char (isDigit)

check x = if isDigit x then 'a' else x

process :: String -> String
process = map check

main :: IO ()
main = do
    contents <- readFile "input1.txt"
    putStr (process contents)

此外,如果您想删除数字,那么filter是比使用map check更好的选择。所以你可以重构进程

process :: String -> String
process = filter (not . isDigit)