加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:54:15 所属栏目:Linux 来源:网络整理
导读:使用C,我可以用通用文本编辑器(例如nano)编写程序,并在Lunix终端中编译 gcc mySum.c -o mySum 获取一个窗口询问输入并返回输出. #include stdio.hint main(){ int x; int y; int sum; printf("Program that sums two numbers.nnPlease type the first one:
使用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.nnPlease 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 %dn",x,y,sum);
return 0;
}

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

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

我会说通过实现这个功能

let mySum x y =
    x + y

fsharpc mySum.fs

但我没有得到如何实现它.我找到了this reference,但它有点先进.

解决方法

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

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

请注意,在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

编辑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:"PathToFSharp.Core.dll" 
 -r:"PathTomscorlib.dll" 
 -r:"PathToYourReferencedOtherDllsSomeClassLib.dll 
 -r:"PathToSystem.Core.dll" 
 -r:"PathToSystem.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#的编辑器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读