F#、Haskell、Scala 和 Visual Basic 或其他函数式编程语言中的两个数字文件 i/o 的简单求和

发布于 2024-10-03 19:50:19 字数 491 浏览 4 评论 0原文

我正在担任算法竞赛的在线评委。我想包括对多种编程语言的支持,但我不了解所有这些语言。我必须制作测试源,但我不知道所有这些语言。

我想要这段代码的等价物:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不羁少年 2024-10-10 19:50:19

Haskell:

main = do
    [astr, bstr] <- fmap lines (readFile "input.txt")
    writeFile "output.txt" $ show (read astr + read bstr)

或者总结所有行:

main = writeFile "output.txt" . show . sum . map read . lines =<< readFile "input.txt"

Haskell:

main = do
    [astr, bstr] <- fmap lines (readFile "input.txt")
    writeFile "output.txt" $ show (read astr + read bstr)

Or to sum all lines:

main = writeFile "output.txt" . show . sum . map read . lines =<< readFile "input.txt"
捂风挽笑 2024-10-10 19:50:19

在 F# 中:

System.IO.File.ReadAllLines "input.txt" |> Seq.sumBy int |> string
|> fun s -> System.IO.File.WriteAllText("output.txt", s)

In F#:

System.IO.File.ReadAllLines "input.txt" |> Seq.sumBy int |> string
|> fun s -> System.IO.File.WriteAllText("output.txt", s)
〃安静 2024-10-10 19:50:19

假设输入文件仅包含两行(带有数字)的 F# 版本:

open System.IO

let [| astr; bstr |] = File.ReadAllLines "input.txt" 
File.WriteAllText("output.txt", string (int astr + int bstr))

这比 Jon 的版本简单一些,但它是一个更直接的解决方案(并且行为与其他人发布的 Haskell 解决方案相同)。

An F# version that assumes that the input file contains just two lines (with numbers):

open System.IO

let [| astr; bstr |] = File.ReadAllLines "input.txt" 
File.WriteAllText("output.txt", string (int astr + int bstr))

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).

獨角戲 2024-10-10 19:50:19

Scala 中有很多方法可以做到这一点。

val f = (as : Array[String]) => as(0).toInt + as(1).toInt
io.Source.fromFile("C:/myfile.xtx").getLines().toStream match {
  case line1 #:: _ => println(f(line1.split(","))
}

你也可以这样做......

val splitLine = (_ : String).split(",")
val printSum = (as : Array[String]) => println(as(0).toInt + as(1).toInt)
val sums = io.Source.fromFile("C:/f.xtx").getLines() map (splitLine andThen printSum)
sums.head //start printing as Iterator is lazy

但是由于通常应该避免副作用,你可能希望使你的函数

val sumLine = (as : Array[String]) => as(0).toInt + as(1).toInt
val sums = io.Source.fromFile("C:/f.xtx").getLines() map (splitLine andThen sumLine)
println(sums.head)

There are a ton of ways of doing this in Scala.

val f = (as : Array[String]) => as(0).toInt + as(1).toInt
io.Source.fromFile("C:/myfile.xtx").getLines().toStream match {
  case line1 #:: _ => println(f(line1.split(","))
}

You could also do...

val splitLine = (_ : String).split(",")
val printSum = (as : Array[String]) => println(as(0).toInt + as(1).toInt)
val sums = io.Source.fromFile("C:/f.xtx").getLines() map (splitLine andThen printSum)
sums.head //start printing as Iterator is lazy

But then since side-effects should generally be avoided, you would probably want to make your functions pure

val sumLine = (as : Array[String]) => as(0).toInt + as(1).toInt
val sums = io.Source.fromFile("C:/f.xtx").getLines() map (splitLine andThen sumLine)
println(sums.head)
懒猫 2024-10-10 19:50:19

这里的一些答案似乎对 C 代码的作用感到困惑。 C 代码并不是特别有用。它需要一个类似的文件

42
1776
this is any sort of random junk
because the program never reads this far

,并生成一个包含

1818

和 的文件,就是这样。在我看来,这是一个展示函数式语言威力的糟糕例子,因为它所做的事情太少了——基本上是一个操作的一个实例。 打哈欠。通过大约相同的工作量,您可以获取一个包含两列数字的文件,并生成一个将它们的总和放在一列中作为输出的文件。只需多做一点工作,您就可以处理输入中任何可能的错误。

但是,公平地说,如果这是手头的工作,那么在 Scala 中完成此任务的一种方法是:

val pw = new java.io.PrintWriter("output.txt")
val num = scala.io.Source.fromFile("input.txt").getLines().map(_.toInt)
pw.print(num.next+num.next)
pw.close  // Only use this line if execution continues (e.g. in REPL)

Some answers here seem confused about what the C code does. The C code is not particularly useful. It takes a file like

42
1776
this is any sort of random junk
because the program never reads this far

and produces a file containing

1818

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:

val pw = new java.io.PrintWriter("output.txt")
val num = scala.io.Source.fromFile("input.txt").getLines().map(_.toInt)
pw.print(num.next+num.next)
pw.close  // Only use this line if execution continues (e.g. in REPL)
百合的盛世恋 2024-10-10 19:50:19

球拍方案:

(define (simpleSum)
  (let* ((input (map string->number (file->lines "input.txt")))
         (a (first input)) (b (second input)))
    (write-to-file (number->string (+ a b)) "output.txt")))

Racket Scheme:

(define (simpleSum)
  (let* ((input (map string->number (file->lines "input.txt")))
         (a (first input)) (b (second input)))
    (write-to-file (number->string (+ a b)) "output.txt")))
你对谁都笑 2024-10-10 19:50:19

我不认为 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 :)

烟雨凡馨 2024-10-10 19:50:19

下面是 VB.NET 的实现:

Dim FileLines() as string = system.io.file.readalllines("input.txt")
system.io.file.WriteAllText("output.txt", ctype(FileLines(0), integer) + ctype(FileLines(1), integer))

Here is the VB.NET implementation:

Dim FileLines() as string = system.io.file.readalllines("input.txt")
system.io.file.WriteAllText("output.txt", ctype(FileLines(0), integer) + ctype(FileLines(1), integer))
少跟Wǒ拽 2024-10-10 19:50:19

Visual Basic 不是函数式语言,除非您使用某些 LINQ 功能。

Visual Basic is not a functional language, unless you use some of the LINQ features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文