使用命令行在Linux中编译F#程序

Wor*_*ice 6 c linux f# command-line-interface read-eval-print-loop

使用C,我可以用通用文本编辑器(ig nano)编写程序,并在Lunix终端中编译

gcc mySum.c -o mySum
Run Code Online (Sandbox Code Playgroud)

获取一个窗口询问输入并返回输出.

#include <stdio.h>

int main(){
        int x;
        int y;
        int sum;
        printf("Program that sums two numbers.\n\nPlease type the first one:\n");
        scanf("%d", &x);
        printf("Type the second one:\n");
        scanf("%d", &y);
        sum = x + y;
        printf("The sum of %d and %d is %d\n", x, y, sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

在没有Visual Studio/MonoDevelopment的情况下,我可以在F#中生成相同类型的程序吗?

我发现使用裸体文本编辑器非常有启发性,就像我在使用C一样.这使我更加专注于学习,而IDE的帮助较少.此外,文本编辑器(如nano或记事本++或其他)提供了比fsharpi编写程序更灵活的工具.通过这种方式,当程序完成时,我将它以与我在示例中使用C相同的方式将其提供给编译器.

我会说通过实现这个功能

let mySum x y =
    x + y
Run Code Online (Sandbox Code Playgroud)

fsharpc mySum.fs
Run Code Online (Sandbox Code Playgroud)

但我没有得到如何实现它.我找到了这个参考,但它有点先进.

Abe*_*bel 5

我认为您想要的是FSI(F#交互式)fsharpi在Linux / MacOS上称为FSI。你也可以使用fsharpc,这将编译它,但如果你只是想出来,测试或脚本则REPL环境,fsharpi让你很可能更方便。

在Linux 上可以找到使用它的介绍。

请注意,在Linux中使用F#进行任何操作之前,必须至少安装了Mono。

编辑:作为说明,您在C中提到的程序可以用多种方式表示,这是一种方式(假定正确的输入或异常,未经测试的程序):

[<EntryPoint>]
let main argv = 
    let scani() =
        printfn "Give a number: "
        Console.ReadLine()
        |> Int64.Parse

    let (x, y) = (scani(), scani())
    printfn "The sum of %d and %d is %d" x y (x + y)

    // wait before returning prompt
    printfn "Hit any key to continue"
    Console.ReadKey() |> ignore
Run Code Online (Sandbox Code Playgroud)

编辑2:您编辑了您的问题,并表示希望与“ Nano”合作。那是我不知道的编辑器。您还说过,您不想使用Mono。我不知道为什么会这样,除非您指的是MonoDevelop,因为没有Mono,您将无法在Linux上编译.NET程序。

您提到的命令gcc mySum.c -o mySum可以转换为F#的命令行变体。例如(假设语法与相同fsc.exe)(为清楚起见,请多行显示):

[<EntryPoint>]
let main argv = 
    let scani() =
        printfn "Give a number: "
        Console.ReadLine()
        |> Int64.Parse

    let (x, y) = (scani(), scani())
    printfn "The sum of %d and %d is %d" x y (x + y)

    // wait before returning prompt
    printfn "Hit any key to continue"
    Console.ReadKey() |> ignore
Run Code Online (Sandbox Code Playgroud)

这些选项中的许多选项可能都可以省略,但这是一个有效的命令行,取自Visual Studio。

但是,我建议您使用预先配置为运行F#程序的编辑器,该编辑器集成了REPL,以及调试器,语法着色,实时类型信息(非常重要!)以及您所拥有的其他智能感知功能。 Visual Studio(VS Code版本在Linux上运行,并且有一个出色的编辑器,要感谢Guy Coder提醒我)和其他一些启用了F#的编辑器。