F#、Haskell、Scala 和 Visual Basic 或其他函数式编程语言中的两个数字文件 i/o 的简单求和
我正在担任算法竞赛的在线评委。我想包括对多种编程语言的支持,但我不了解所有这些语言。我必须制作测试源,但我不知道所有这些语言。
我想要这段代码的等价物:
#include <stdio.h>
int main () {
int a, b;
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d%d", &a, &b);
printf("%d", a + b);
return 0;
}
用这种编程语言。
我想从文件 input.txt 读取两个数字,每个数字占一行,并将它们的总和写入输出文件 output.txt
谢谢。
编辑 请不要告诉我 Visual Basic .NET 不是一种函数式语言。我知道这。
I'm working on an online judge for algorithm contests. I want to include support for many programming languages, but i don't know all of them. I have to make test sources, but i don't know all of these languages.
I want the equivalent of this code:
#include <stdio.h>
int main () {
int a, b;
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d%d", &a, &b);
printf("%d", a + b);
return 0;
}
In this programming languages.
I want to read from file input.txt two numbers, each on a line, and write the sum of them to the output file output.txt
Thank you.
EDIT Please don't tell me that Visual Basic .NET is not a functional language. I know it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Haskell:
或者总结所有行:
Haskell:
Or to sum all lines:
在 F# 中:
In F#:
假设输入文件仅包含两行(带有数字)的 F# 版本:
这比 Jon 的版本简单一些,但它是一个更直接的解决方案(并且行为与其他人发布的 Haskell 解决方案相同)。
An F# version that assumes that the input file contains just two lines (with numbers):
This is a bit simpler than Jon's version, but it is a more direct solution (and behaves the same as Haskell solution posted by others).
在 Scala 中有很多方法可以做到这一点。
你也可以这样做......
但是由于通常应该避免副作用,你可能希望使你的函数纯
There are a ton of ways of doing this in Scala.
You could also do...
But then since side-effects should generally be avoided, you would probably want to make your functions pure
这里的一些答案似乎对 C 代码的作用感到困惑。 C 代码并不是特别有用。它需要一个类似的文件
,并生成一个包含
和 的文件,就是这样。在我看来,这是一个展示函数式语言威力的糟糕例子,因为它所做的事情太少了——基本上是一个操作的一个实例。 打哈欠。通过大约相同的工作量,您可以获取一个包含两列数字的文件,并生成一个将它们的总和放在一列中作为输出的文件。只需多做一点工作,您就可以处理输入中任何可能的错误。
但是,公平地说,如果这是手头的工作,那么在 Scala 中完成此任务的一种方法是:
Some answers here seem confused about what the C code does. The C code is not particularly useful. It takes a file like
and produces a file containing
and that is it. IMO, this is a lousy example for showing the power of functional languages because it does so little--one instance of one operation, basically. Yawn. With about the same amount of work, you could take a file with two columns of numbers and produce a file that had their sum in one column as an output. With a tiny bit more work, you could handle any conceivable error in the input.
But, fair enough, if this is the job at hand, one way to accomplish this in Scala is:
球拍方案:
Racket Scheme:
我不认为 scala 在如此简单的场景中具有其优势,包括其所有花哨的功能和 api。最好使用java库。 java.util.Scanner提供了有用的nextInt(),它可以在很多情况下工作,而PrintWriter是java中最快的输出机制。
val in = new Scanner(new FileInputStream("input.txt"))
val out = new PrintWriter("output.txt")
out.println(( in.nextInt + in.nextInt))
out.close
in.close
另外,我很好奇您正在处理哪个在线法官: )
I don't think scala has its advantage in such simple scenario, including all its fancy features and its api. It's better to use java library. java.util.Scanner has provided the useful nextInt() which can work in many cases and PrintWriter is the fastest output mechniasm in java.
val in = new Scanner(new FileInputStream("input.txt"))
val out = new PrintWriter("output.txt")
out.println((in.nextInt + in.nextInt))
out.close
in.close
And also, I'm curious about which online judge you're working on :)
下面是 VB.NET 的实现:
Here is the VB.NET implementation:
Visual Basic 不是函数式语言,除非您使用某些 LINQ 功能。
Visual Basic is not a functional language, unless you use some of the LINQ features.