Haskell:继续执行程序 [英] Haskell: Continue program execution

查看:66
本文介绍了Haskell:继续执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Haskell非常陌生.我的问题对您来说可能是非常基本的.我去了-

I am very new to Haskell. My question might be very basic for you. Here I go-

我正在编写一个程序,使用特定的数学公式来创建一系列数字.创建完这个系列后,我应该对其进行一些操作,例如从这些数字中找出最大值/最小值.

I am writing a program to create a series of numbers using a specific mathematical formula. After creating this series, I am supposed to perform some operation on it like finding the maximum/minimum out of those numbers.

我可以编写程序,但是在从用户获得单次输入后,我的程序显示输出,然后退出.如果我必须等待来自用户的更多命令并在命令END上退出,该怎么办?

I could write the program but after getting a single input from the user, my program displays the output and then exits. What should I do if I have to wait for more commands from the user and exit on command END?

line <- getLine

我正在使用此命令获取命令,然后根据该命令调用必要的功能.我应该如何进行?

I am using this command to get a command and then calling the necessary function according to the command. How should I proceed?

推荐答案

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.

这篇关于Haskell:继续执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆