且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Haskell:继续执行程序

更新时间:2023-01-14 14:34:20

Prelude.interact :

calculate :: String -> String
calculate input =
  let ws = words input
  in  case ws of
        ["add", xs, ys] -> show $ (read xs) + (read ys)
        _ -> "Invalid command"

main :: IO ()
main = interact calculate

交互::(String-> String)-> IO() interact函数将String-> String类型的函数作为参数.标准输入设备的全部输入作为其参数传递给此函数,结果字符串在标准输出设备上输出.

interact :: (String -> String) -> IO () The interact function takes a function of type String->String as its argument. The entire input from the standard input device is passed to this function as its argument, and the resulting string is output on the standard output device.