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

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

问题描述

对于Haskell我很新。我的问题对你来说可能是非常基础的。在这里,我去 -
我正在编写一个程序,使用特定的数学公式创建一系列数字。在创建这个系列之后,我应该对它进行一些操作,比如从这些数字中找出最大值/最小值。
所以,我可以编写程序,但在得到用户的单个输入后,我的程序显示输出并退出。如果我必须等待更多用户命令并退出命令END,我该怎么办?

line< - getLine



我使用这个命令来获得一个命令,然后根据命令调用必要的函数。我应该如何继续?

解决方案

这是 Prelude.interact

  calculate :: String  - >字符串
计算输入=
让ws =字输入
(如果ws为
[add,xs,ys] - > show $(read xs)+(read ys)
_ - > 无效的命令

main :: IO()
main = interact计算




交互::(字符串 - >字符串) - > IO()交互函数接受一个String-> String类型的函数作为它的参数。标准输入设备的全部输入作为其参数传递给该函数,并在标准输出设备上输出结果字符串。



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 number 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. So, I could write the program but after getting a single input from user, my program displays the output and then exits. What should I do if I have to wait for more commands from 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?

解决方案

There is Prelude.interact for this:

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

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天全站免登陆