Haskell interact function

后端 未结 2 1394
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 17:18

I’m new to Haskell and have a problem with interact function. This is my sample program:

main :: IO ()
main = interact inputLength

inputLength          


        
2条回答
  •  醉话见心
    2020-12-14 17:52

    A more reusable solution:

    main = interactLineByLine processLine
    
    -- this wrapper does the boring thing of mapping, unlining etc.... you have to do all the times for user interaction
    interactLineByLine:: (String -> String) -> IO ()
    interactLineByLine f = interact (unlines . (map processLine) . lines) 
    
    -- this function does the actual work line by line, i.e. what is
    -- really desired most of the times
    processLine:: String -> String
    processLine line = "<" ++ line ++ ">"
    

提交回复
热议问题