小编ICT*_*lor的帖子

如何使用stdin中的数据执行某些操作,逐行,最多次并打印Haskell中的行数

此代码从stdin的第一行读取要处理的行数,然后循环number_of_lines_to_process次,进行一些计算并打印结果.我希望它在"#"之后的"Line#"中打印行号,但我不知道如何获取它

import IO
import Control.Monad (replicateM)

main :: IO ()

main = do
    hSetBuffering stdin LineBuffering
    s <- getLine
    let number_of_lines_to_process = read s :: Integer
    lines <- replicateM (fromIntegral(number_of_lines_to_process)) $ do
        line <- getLine
        let number = read line :: Integer
            result = number*2 --example
        putStrLn ("Line #"++": "++(show result)) --I want to print the number of the iteration and the result
    return ()
Run Code Online (Sandbox Code Playgroud)

我想这个问题的解决方案非常简单,但我不熟悉Haskell(第一次在其中编码),我没有找到任何办法.有人可以帮忙吗?

monads haskell

6
推荐指数
3
解决办法
703
查看次数

标签 统计

haskell ×1

monads ×1