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

标签 c linux f# command-line-interface read-eval-print-loop

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

gcc mySum.c -o mySum

获取一个窗口,要求输入并返回输出。

#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 Studio/MonoDevelopment 的情况下用 F# 生成相同类型的程序吗?

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

我会说通过实现函数

let mySum x y =
    x + y

fsharpc mySum.fs

但我不知道如何实现它。我找到了 this reference但它有点先进。

最佳答案

我想你想要的是FSI (F# Interactive) ,在 Linux/MacOS 上称为 fsharpi。您也可以使用 fsharpc,这将编译它,但如果您只是尝试、测试或编写脚本,那么 REPL environment fsharpi 给你的可能更方便。

在 Linux 中使用它的介绍 can be found here .

请注意,您必须至少安装 Mono,然后才能在 Linux 中使用 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# 的编辑器。

关于在 Linux 中使用命令行编译 F# 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41227922/

相关文章:

c - 无损压缩算法是否在位级别上工作?

linux - 将 "mail -a"与 $Sender var 一起使用

f# - 主动识别器的推断类型 - 一个是选项,另一个不是

c - 如何打印另一个进程的信息?

C:输出带有凯撒密码加密中的符号,为什么? pset2 CS50

c - 如何查看 #include 包含的 header 的位置?

linux - 如何只允许 samba 通过 vpn 隧道?

c - epoll_wait 接收未知事件

.net - 有没有办法四舍五入到小数点前 .net 中的 3 个有效数字(特别是 f#)?

wpf - 在 .fs 文件中引用 .xaml 文件中的事件