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

Compile F# program in Linux using command line

使用 C,我可以使用通用文本编辑器 (i.g.nano) 编写程序并使用

在 Lunix 终端中编译它
gcc mySum.c -o mySum

得到一个window请求输入并返回输出。

#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;
}

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

我发现使用裸文本编辑器对我很有启发性,就像我使用 C 一样。它迫使我更加专注于学习,IDE 的帮助更少。此外,文本编辑器(例如 nano、notepad++ 或其他)提供了比 fsharpi 更灵活的工具来编写程序。这样,当程序完成后,我把它交给编译器,就像我在例子中对 C 所做的那样。

我会说通过实现函数

let mySum x y =
    x + y

fsharpc mySum.fs

但我不知道如何实现它。我找到了this reference,但有点高级。

我想你想要的是FSI (F# Interactive), which is called fsharpi on Linux / MacOS. You can also use fsharpc, which will compile it, but if you are just trying out, testing or scripting then the REPL environmentfsharpi给你的可能更方便。

使用介绍来自 Linux can be found here

请注意,在 Linux.

中,您必须至少安装 Mono 才能使用 F# 执行任何操作

编辑:如图所示,您在 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

编辑 2:您编辑了问题并表达了与 "Nano" 合作的意愿。那是一个我不认识的编辑器。您还说您不想使用 Mono。我不知道为什么会这样,除非你指的是 MonoDevelop,因为如果没有 Mono,你就无法在 Linux.

上编译 .NET 程序

您提到的命令 gcc mySum.c -o mySum 可以翻译成 F# 的命令行变体。例如(假设语法与 fsc.exe 相同)(为清楚起见放在多行中):

fsharpc.exe 
 -o:MyProgram.exe 
 --debug:pdbonly 
 --noframework 
 --optimize+ 
 --platform:anycpu32bitpreferred 
 -r:"PathTo\FSharp.Core.dll" 
 -r:"PathTo\mscorlib.dll" 
 -r:"PathToYourReferencedOtherDlls\SomeClassLib.dll 
 -r:"PathTo\System.Core.dll" 
 -r:"PathTo\System.dll"
 --target:exe 
 --warn:3 
 file1.fs file2.fs file3.fs 

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

不过,我建议使用预配置为 运行 F# 程序的编辑器,它集成了 REPL,以及调试器、语法着色、实时类型信息(非常重要!)和其他智能感知功能,您可以通过 Visual Studio(VS Code edition runs on Linux 获得出色的编辑器,感谢 Guy Coder 提醒我)和可能的其他一些 F#启用编辑器。