如何在Ubuntu 18.04上安装F#并设置本地编程环境

罗凯
2023-12-01

The author selected the Free Software Foundation to receive a donation as part of the Write for DOnations program.

作者选择了自由软件基金会作为Write for DOnations计划的一部分接受捐赠。

介绍 (Introduction)

F# is an open-source programming language initially developed at Microsoft Research to extend .NET, Microsoft’s set of tools, libraries, and languages to build applications and services. Besides its remarkably concise syntax, F# supports multiple paradigms, meaning that it can do different types of code structuring, though it was primarily designed to take advantage of the functional programming approach.

F#是一种开源编程语言,最初由Microsoft Research开发,用于扩展.NET (Microsoft的工具,库和语言集)以构建应用程序和服务。 除了其简洁的语法外,F#还支持多种范例 ,这意味着F#可以执行不同类型的代码结构,尽管它主要是为了利用功能性编程方法而设计的。

Adopting a specific paradigm, or a style of code, determines the way we will think and organize our programming problem solving. With an imperative approach, the design model used in languages like C++ or Java, a developer describes step-by-step how the computer must accomplish a task. It’s about writing a sequence of statements that will change memory states at the program’s execution. This works fine until we encounter some irregular situations. Consider a shared object for instance, which is used by multiple applications simultaneously. We might want to read its value at the same time that another component is modifying it. These are concurrent actions upon a memory location that can produce data inconsistency and undefined behavior.

采用特定的范例或代码样式,将决定我们思考和组织编程问题的方式。 通过命令式方法 (用于C ++Java等语言的设计模型),开发人员逐步描述了计算机如何完成任务。 这是关于编写一系列语句,这些语句将在程序执行时更改内存状态。 直到遇到一些不正常的情况,此方法才能正常工作。 例如,考虑一个共享对象 ,该对象同时被多个应用程序使用。 我们可能想在另一个组件修改它的同时读取它的值。 这些是对内存位置的并发操作 ,可能会导致数据不一致和不确定的行为。

In functional code design, we prevent this kind of problem by minimizing the use of mutable states, or states that can change after we make them. Function is the keyword here, referring to mathematical transformations on some information provided as arguments. A functional code expresses what the program is by composing the solution as a set of functions to be executed. Typically, we build up layers of logic using functions that can return another function or take other functions as inputs.

功能代码设计中 ,我们通过最大限度地减少使用可变状态 (或可变状态)来避免此类问题。 函数是此处的关键字,是指对作为参数提供的某些信息进行数学转换。 功能代码通过将解决方案组合为要执行的一组功能来表达程序的含义。 通常,我们使用可以返回另一个函数或将其他函数作为输入的函数来构建逻辑层。

Functional programming with F# brings a number of benefits:

使用F#进行函数式编程带来了许多好处:

  • A more readable and expressive syntax that increases program maintainability.

    一种更具可读性和表达力的语法,可以提高程序的可维护性。
  • A code less prone to breaking and easier to debug because of stateless functions that can be isolated for testing.

    由于可以隔离进行测试的无状态功能,因此代码不易中断,更易于调试。
  • Native constructs that facilitate asynchronous programming and safer concurrency.

    有助于异步编程和更安全的并发性的本机结构。
  • Access to all the existing tools in the .NET world including the community-shared packages.

    访问.NET世界中所有现有工具,包括社区共享的软件包。

选择运行时 (Choosing a Runtime)

Since F# is cross-platform, maintaining a similar execution model behavior through different operating systems is essential. .NET achieves this by means of a runtime. A runtime system is a piece of software that orchestrates the execution of a program written with a specific programming language, handling interfacing with the operating system and memory management, among other things.

由于F#是跨平台的,因此必须通过不同的操作系统维护相似的执行模型行为。 .NET通过运行时来实现。 运行时系统是一种软件,它协调用特定编程语言编写的程序的执行,处理与操作系统的接口以及内存管理等。

There are actually two .NET runtime implementations available on Linux: .NET Core and Mono. Historically, .NET only worked on Windows. In those days, one could resort to the community Mono project to run .NET applications on other platforms like Linux and macOS. Microsoft then launched .NET Core, a faster, modular subset of the original .NET framework, to target multiple platforms.

在Linux上实际上有两种.NET运行时实现: .NET CoreMono 。 过去,.NET仅在Windows上运行。 在那些日子里,人们可以求助于Mono项目社区,以便在Linux和macOS等其他平台上运行.NET应用程序。 然后,Microsoft启动了.NET Core,它是原始.NET框架的一个更快的模块化子集,可用于多个平台。

At the time of this tutorial’s publication, they both can be used for building web applications or command line utilities. That said, .NET Core does not ship models to create GUI desktop applications on Linux and macOS, while Mono is the only one to support mobile and gaming platforms. It is important to know these differences since the runtime you pick will shape the programs you will build. You could also choose to have both .NET Core and Mono installed in order to account for all use cases and to make a more productive stack.

在本教程出版时,它们都可用于构建Web应用程序或命令行实用程序。 也就是说,.NET Core并未提供用于在Linux和macOS上创建GUI桌面应用程序的模型,而Mono是唯一支持移动和游戏平台的模型。 了解这些差异很重要,因为您选择的运行时将影响要构建的程序。 您也可以选择同时安装.NET Core和Mono,以解决所有用例并提高生产效率。

In this tutorial, you will set up an F# programming environment on Ubuntu 18.04 using both .NET Core and Mono runtimes. You will then write some code examples to test and review build and compile methods.

在本教程中,您将使用.NET Core和Mono运行时在Ubuntu 18.04上设置F#编程环境。 然后,您将编写一些代码示例来测试和检查构建和编译方法。

先决条件 (Prerequisites)

To complete this tutorial, you will need basic familiarity with the command line and a computer running Ubuntu 18.04 with a non-root user with sudo privileges.

要完成本教程,您需要基本熟悉命令行,以及一台使用具有sudo特权非root用户运行Ubuntu 18.04的计算机。

第1步-使用.NET Core安装F# (Step 1 — Installing F# with .NET Core)

Microsoft provides the .NET Core Software Development Kit (SDK) for F# developers. A Software Development Kit is a set of programming tools that allows programmers to produce specialized applications and adapt them to various operating systems. It traditionally includes a text editor, languages support, a runtime, and a compiler, among other components. In this step, you are going to install this SDK. But first, you will register the Microsoft repository and fetch some dependencies.

Microsoft为F#开发人员提供了.NET Core软件开发工具包(SDK) 。 软件开发工具包是一组编程工具,使程序员可以生成专门的应用程序并使它们适应各种操作系统。 传统上,它包括文本编辑器,语言支持,运行时和编译器以及其他组件。 在此步骤中,您将安装此SDK。 但是首先,您将注册Microsoft存储库并获取一些依赖项。

You’ll be completing the installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

您将在命令行上完成安装和设置,这是与计算机交互的非图形方式。 也就是说,您将不用输入按钮,而是输入文本,并通过文本接收来自计算机的反馈。

The command line, also known as a shell or terminal, can help modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. For more information about the command line, check out the Introduction to the Linux Terminal tutorial.

命令行也称为外壳程序终端 ,可以帮助您修改和自动化您每天在计算机上执行的许多任务,并且是软件开发人员的必备工具。 有许多要学习的终端命令,可以使您执行更强大的功能。 有关命令行的更多信息,请查看《 Linux Terminal简介》教程。

On Ubuntu 18.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on the keyboard at the same time to open the Terminal application automatically.

在Ubuntu 18.04上,您可以通过单击屏幕左上角的Ubuntu图标,然后在搜索栏中键入terminal来找到Terminal应用程序。 单击终端应用程序图标以将其打开。 或者,您可以同时按下键盘上的CTRLALTT键以自动打开“终端”应用程序。

Once you have opened the terminal, use the wget command to download a package containing some required files, the Microsoft repository configurations, and a key for server communication.

打开终端后,使用wget命令下载包含一些必需文件,Microsoft系统信息库配置和服务器通信密钥的软件包。

  • wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

    wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

Now, add the Microsoft repository and install the packages to your system using the dpkg -i instruction.

现在,添加Microsoft系统信息库,并使用dpkg -i指令将软件包安装到系统中。

  • sudo dpkg -i packages-microsoft-prod.deb

    sudo dpkg -i软件包-microsoft-prod.deb

Next, activate the Universe repository, which on Ubuntu is a community-maintained archive of software that is free and open source. This will give you access to apt-transport-https, a dependency for enabling the Ubuntu package manager APT transport over HTTPS.

接下来,激活Universe存储库,该存储库在Ubuntu上是社区维护的免费开放源代码软件档案。 这将使您能够访问apt-transport-https ,这是启用Ubuntu软件包管理器APT通过HTTPS传输的依赖项。

  • sudo add-apt-repository universe

    sudo add-apt-repository Universe
  • sudo apt install apt-transport-https

    sudo apt安装apt-transport-https

Next, update available downloads:

接下来,更新可用下载:

  • sudo apt update

    sudo apt更新

Finally, install the current version of the .NET SDK. This tutorial will use version 2.2:

最后,安装当前版本的.NET SDK。 本教程将使用2.2版:

  • sudo apt install dotnet-sdk-2.2

    须藤apt install dotnet-sdk- 2.2

Now that you have the .NET SDK installed, a quick way to check if everything went well is to try the .NET Core command line interface (CLI), which will be available in the shell once the SDK is downloaded and installed. Display information about your .NET setup by typing this in your terminal:

现在,您已经安装了.NET SDK,一种检查是否一切顺利的快速方法是尝试.NET Core命令行界面(CLI),一旦下载并安装了SDK,它将在外壳程序中可用。 通过在终端中键入以下内容来显示有关.NET设置的信息:

  • dotnet --info

    dotnet --info

When you run a dotnet command for the first time, a text section is displayed as shown below:

首次运行dotnet命令时,将显示一个文本部分,如下所示:


   
   
Output
Welcome to .NET Core! --------------------- Learn more about .NET Core: https://aka.ms/dotnet-docs Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs Telemetry --------- The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous and doesn't include command-line arguments. The data is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell. Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry ...

This notification is about collected data, and explains that some .NET CLI commands will send usage information to Microsoft. You will disable this in a moment; for now, look at the output from dotnet --info.

该通知与收集的数据有关,并说明某些.NET CLI命令会将使用信息发送给Microsoft。 您稍后将禁用此功能; 现在,来看一下dotnet --info的输出。

After a brief moment, the terminal will list information about your .NET installation:

片刻之后,终端将列出有关.NET安装的信息:


   
   
Output
.NET Core SDK (reflecting any global.json): Version: 2.2.101 Commit: 236713b0b7 Runtime Environment: OS Name: ubuntu OS Version: 18.04 OS Platform: Linux RID: ubuntu.18.04-x64 Base Path: /usr/share/dotnet/sdk/2.2.101/ Host (useful for support): Version: 2.2.0 Commit: 1249f08fed .NET Core SDKs installed: 2.2.101 [/usr/share/dotnet/sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.2.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.2.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.2.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download

Depending on the SDK version, the output may be slightly different, but this confirms that .NET Core is ready to use.

根据SDK版本的不同,输出可能会略有不同,但这可以确认.NET Core已可以使用。

As mentioned before, the telemetry feature allows some .NET CLI commands to send usage information to Microsoft. It is enabled by default, and can be deactivated by setting the DOTNET\_CLI\_TELEMETRY_OPTOUT environment variable to 1. To do so, add a new line to your .profile environment customization file by opening it in your text editor. For this tutorial, we will use nano:

如前所述,遥测功能允许某些.NET CLI命令将使用信息发送给Microsoft。 默认情况下启用它,可以通过将DOTNET\_CLI\_TELEMETRY_OPTOUT环境变量设置为1来禁用它。 这样做,通过在文本编辑器中打开.profile环境自定义文件添加新行。 在本教程中,我们将使用nano

  • nano ~/.profile

    纳米〜/ .profile

Add the following line to the end of .profile:

.profile下行添加到.profile的末尾:

~/.profile
〜/ .profile
. . .
export DOTNET_CLI_TELEMETRY_OPTOUT=1

Exit nano by pressing the CTRL and X keys. When prompted to save the file, press Y and then ENTER.

通过按CTRLX键退出nano 。 当提示您保存文件时,按Y ,然后按ENTER

You can activate the new configuration using the source command:

您可以使用source命令激活新配置:

  • source ~/.profile

    来源〜/ .profile

From now on, telemetry will be turned off at startup.

从现在开始,遥测将在启动时关闭。

At this point you have .NET Core runtime, languages support, and libraries installed, allowing you to run and build some .NET applications. The dotnet CLI is also available for managing .NET source code and binaries. You could start building F# projects, but as mentioned previously, the .NET Core environment does not provide all the constructs needed to be completely cross-platform. For now you cannot use it to develop mobile applications, for example.

至此,您已经安装了.NET Core运行时,语言支持和库,使您可以运行和构建某些.NET应用程序。 dotnet CLI也可用于管理.NET源代码和二进制文件。 您可以开始构建F#项目,但是如前所述,.NET Core环境并未提供完全跨平台所需的所有构造。 例如,目前您不能使用它来开发移动应用程序。

In order to solve this problem, in the next step you will install F# again, but this time with Mono.

为了解决此问题,在下一步中,您将再次安装F#,但这一次是使用Mono。

第2步-使用Mono安装F# (Step 2 — Installing F# with Mono)

You can use Mono to fill in the remaining gaps in capability left by .NET Core. Mono and .NET Core are both based on the same standard library and both support .NET languages, but that is where the similarity ends. They use different runtimes, different CLIs, and different compilers, making it possible for them to be installed side by side to create a more reliable programming environment. In this section you are going to supplement your environment with the Mono tools for .NET programming and run an F# program from the command line.

您可以使用Mono来填补.NET Core所剩功能的不足。 Mono和.NET Core都基于相同的标准库,并且都支持.NET语言,但这就是相似之处的所在。 他们使用不同的运行时,不同的CLI和不同的编译器,从而可以并行安装它们以创建更可靠的编程环境。 在本节中,您将使用用于.NET编程的Mono工具补充您的环境,并从命令行运行F#程序。

A version of Mono is available in the Ubuntu repositories, but this can be outdated. Instead, add the official Mono package repository to your package manager:

Ubuntu存储库中提供了Mono版本,但这可能已过时。 相反,将官方的Mono软件包存储库添加到您的软件包管理器中:

  • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
  • echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list

    回声“ deb https://download.mono-project.com/repo/ubuntu stable-bionic主” | sudo tee /etc/apt/sources.list.d/mono-official-stable.list

In the preceding commands, you used apt-key to retrieve keys for securing packages transferred from the official Mono repositories. You then added the Mono packages source to your repositories list.

在前面的命令中,您使用apt-key检索密钥来保护从官方Mono存储库传输来的软件包的安全。 然后,您将Mono软件包源添加到了存储库列表中。

With a new source list added for APT, update your repositories:

在为APT添加新的来源列表之后,更新您的存储库:

  • sudo apt update

    sudo apt更新

Next, download the Mono tools. Unlike .NET Core, Mono does not include F# tools, so you will download it as a separate package. Install fsharp and the mono-complete meta-package using the following command:

接下来,下载Mono工具。 与.NET Core不同,Mono不包含F#工具,因此您将以单独的程序包形式下载它。 使用以下命令安装fsharpmono-complete元包:

  • sudo apt install mono-complete fsharp

    sudo apt安装单声道完整fsharp

Note: Because of the size of this download, the installation process for mono-complete may take a while.

注意:由于此下载文件的大小, mono-complete的安装过程可能需要一段时间。

Once done, you will have the compiler fsharpc and an interactive shell called fsharpi or simply FSI. FSI is an environment, inside the shell, that receives user’s input as an expression, evaluates it, then outputs the result and waits for another input. It is just like typing a command in the traditional shell and seeing the result, except here, inputs are F# expressions. FSI provides a fast method to test code or run scripts.

完成后,您将拥有编译器fsharpc和一个名为fsharpi或简称为FSI的交互式外壳。 FSI是外壳内部的一个环境,该环境接收用户的输入作为表达式,对其进行求值,然后输出结果并等待其他输入。 就像在传统的shell中键入命令并查看结果一样,除了这里的输入是F#表达式。 FSI提供了一种快速的方法来测试代码或运行脚本。

Activate FSI with the following command:

使用以下命令激活FSI:

  • fsharpi

    沙皮

This will start the interactive session and replace your regular prompt with the fsharpi prompt:

这将启动交互式会话,并用fsharpi提示符替换您的常规提示符:


   
   
Output
Microsoft (R) F# Interactive version 4.1 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; >

You can return to the default shell by running #quit;;. In fsharpi, each command line ends with a double semicolon.

您可以通过运行#quit;;返回默认的shell。 。 在fsharpi ,每个命令行以双分号结尾。

Let’s try a simple operation using the printfn function to render a message passed as a parameter:

让我们尝试使用printfn函数进行简单的操作,以呈现作为参数传递的消息:

  • printfn "Hello World!";;

    printfn“ Hello World!” ;;

You will receive the following output:

您将收到以下输出:


   
   
Output
Hello World! val it : unit = () >

From the preceding interaction, fsharpi evaluates the expression as a unit type value. The code is then executed and the result is printed with its type.

通过前面的交互, fsharpi将表达式评估为unit类型值。 然后执行代码,并以其类型打印结果。

fsharpi can also run a file containing F# code. The script must be named with a .fsx extension and executed from the shell with the command:

fsharpi也可以运行包含F#代码的文件。 该脚本必须以.fsx扩展名命名,并使用以下命令从Shell中执行:

  • fsharpi some_script.fsx

    fsharpi some_script .fsx

Now that you know the F# installation is working, leave the shell with:

既然您知道F#安装正在工作,那么将Shell保留为:

> #quit;;

With Mono and .NET Core installed, you are now prepared to write any type of F# programs. FSI will allow you to test your code and run some scripts if needed, but executions will be slow. For your F# script to be executed, additional steps are performed to translate the source code into artifacts understandable by the processor, hence the slowness. To remedy this, in the next section you will compile your code with .NET Core, creating standalone binary files that can be immediately run by the machine.

安装了Mono和.NET Core后,您现在就可以编写任何类型的F#程序了。 FSI将允许您测试代码并在需要时运行一些脚本,但是执行速度会很慢。 对于要执行的F#脚本,需要执行其他步骤以将源代码转换为处理器可以理解的工件,因此速度较慢。 为了解决这个问题,在下一部分中,您将使用.NET Core编译代码,创建可以由计算机立即运行的独立二进制文件。

第3步-使用.NET Core编写和编译F#程序 (Step 3 — Writing and Compiling F# Programs with .NET Core)

In this step, you will compile F# source code via command line compilers provided with .NET Core. This will allow you to make your applications faster and to produce preset executable packages for specific systems, making your program easier to distribute.

在此步骤中,您将通过.NET Core附带的命令行编译器来编译F#源代码。 这样可以使您的应用程序更快,并为特定系统生成预设的可执行程序包,从而使程序更易于分发。

Compiling is the transformation process that turns source code into binary file. The software that accomplishes this conversion is called a compiler. .NET Core relies on the dotnet CLI to perform compiling. To demonstrate this, you are going to create a basic F# source to review the compilation cases.

编译是将源代码转换为二进制文件的转换过程。 完成此转换的软件称为编译器。 .NET Core依靠dotnet CLI进行编译。 为了证明这一点,您将创建一个基本的F#源代码来检查编译案例。

The dotnet CLI provides a complete application build toolchain. In general, an association of a command and the dotnet driver is used in the shell to complete a task. For example:

dotnet CLI提供了完整的应用程序构建工具链。 通常,外壳中使用命令和dotnet驱动程序的关联来完成任务。 例如:

  • dotnet new will create a project

    dotnet new将创建一个项目

  • dotnet build will build a project and all of its dependencies

    dotnet build将构建一个项目及其所有依赖项

  • dotnet add package will add a package reference to a project file

    dotnet add package会将软件包引用添加到项目文件

The following will create a new console project called FSharpHello. The -lang option sets the programming language you will code with while the -o option creates a directory in which to place the output.

下面将创建一个名为FSharpHello的新控制台项目。 -lang选项设置您将使用的编程语言,而-o选项创建用于放置输出的目录。

  • dotnet new console -lang F# -o FSharpHello

    dotnet新控制台-lang F#-o FSharpHello

Once this is done, navigate into your newly created project directory:

完成此操作后,导航到新创建的项目目录:

  • cd FSharpHello

    光盘FSharpHello

This directory contains the FSharpHello.fsproj project configuration file and the obj folder which is used to store temporary object files. There is also the Program.fs file where your default source code exists. Open it in your text editor:

此目录包含FSharpHello .fsproj项目配置文件和obj文件夹,该文件夹用于存储临时对象文件。 还有默认源代码所在的Program.fs文件。 在您的文本编辑器中打开它:

  • nano Program.fs

    纳米程序

The file has been automatically filled with a Hello World program:

该文件已自动使用Hello World程序填充:

Program.fs
程序
// Learn more about F# at http://fsharp.org

open System

[<EntryPoint>]
let main argv =
    printfn "Hello World from F#!"
    0 // return an integer exit code

In this code, you start importing the System module with open System, then you define the program entry point, i.e., the place where the program starts when launched from the shell. The main function will call for a Hello World message printing to the console and will stop the program (return an integer exit code).

在此代码中,您将开始使用open System导入System模块,然后定义程序入口点,即从shell启动程序时程序开始的位置。 main功能将要求在控制台上打印Hello World消息,并将停止程序( return an integer exit code )。

Exit out of the file.

退出文件。

To compile and run this code, use the following from the project directory ~/FSharpHello:

要编译并运行此代码,请在项目目录~/ FSharpHello使用以下代码:

  • dotnet run

    网络运行

The program will run, printing the following output to the screen:

该程序将运行,将以下输出打印到屏幕上:


   
   
Output
Hello World from F#!

Note that it took a while for this program to run, just as with the FSI. As we mentioned before, it’s possible to run this faster by generating an executable, i.e., a binary file that can be run directly by the operating system. Here is how to achieve this:

请注意,与FSI一样,该程序也需要花费一些时间才能运行。 如前所述,可以通过生成可执行文件(即可以由操作系统直接运行的二进制文件)来更快地运行它。 这是实现此目的的方法:

  • dotnet publish -c release -r linux-x64

    dotnet发布-c版本-r linux-x64

This will produce the executable bin/release/netcoreapp2.2/linux-x64/publish/FSharpHello.dll file. This is a shared library that will run on a 64-bit Linux architecture. To export a generic executable for macOS systems, you would replace the linux-x64 runtime identifier (RID) with osx-x64.

这将生成可执行文件bin/release/netcoreapp 2.2 /linux-x64/publish/ FSharpHello .dll文件。 这是一个共享库,将在64位Linux体系结构上运行。 要为macOS系统导出通用可执行文件,您可以将linux-x64运行时标识符( RID )替换为osx-x64

Now execute the file with the following command:

现在,使用以下命令执行文件:

  • dotnet bin/release/netcoreapp2.2/linux-x64/publish/FSharpHello.dll

    dotnet bin / release / netcoreapp 2.2 / linux-x64 / publish / FSharpHello .dll

This time, you will receive the output much quicker, since the program is already translated into binary.

这次,您将更快地收到输出,因为该程序已经转换为二进制文件了。

Now that you know how to compile in .NET Core, let’s see how Mono compiles programs with the dedicated fsharpc command.

现在,您知道如何在.NET Core中进行编译,让我们看看Mono如何使用专用的fsharpc命令来编译程序。

第4步-使用Mono编写和编译F#程序 (Step 4 — Writing and Compiling F# Programs with Mono)

Mono’s compilation process is similar to that of .NET Core, but this time there is a specific command used to compile the program. The fsharpc command is the tool, and it has been created only for compiling.

Mono的编译过程与.NET Core相似,但是这次有一个特定的命令用于编译程序。 fsharpc命令是工具,并且仅用于编译而创建。

This time, create a hello.fs file and write some F# code. First, return to your home directory:

这次,创建一个hello.fs文件并编写一些F#代码。 首先,返回您的主目录:

  • cd

    光盘

Next, open up a new file named hello.fs:

接下来,打开一个名为hello.fs的新文件:

  • nano hello.fs

    纳米hello.fs

Add the following line to the file:

将以下行添加到文件中:

hello.fs
你好
open System

As seen before, this imports the System module or namespace, giving you access to built-in system functions and objects like Console.

如前所示,这将导入System模块或名称空间,使您可以访问内置系统功能和对象(例如Console

Now, add in some more lines of code:

现在,添加更多代码行:

hello.fs
你好
open System

let hello() =
    printf "Who are you? "
    let name = Console.ReadLine()
    printfn "Oh, Hello %s!\nI'm F#." name

These new lines define the hello() function to read user input and print a feedback message.

这些新行定义了hello()函数,以读取用户输入并打印反馈消息。

Now you can add the final lines:

现在您可以添加最后几行:

hello.fs
你好
open System

let hello() =
    printf "Who are you? "
    let name = Console.ReadLine()
    printfn "Oh, Hello %s!\nI'm F#." name

hello()
Console.ReadKey() |> ignore

Here you are calling the function hello(), then using the ReadKey() method to end the program with a final keystroke.

在这里,您要调用函数hello() ,然后使用ReadKey()方法以最后的击键结束程序。

Save and exit the file.

保存并退出文件。

Now with the fsharpc command, use the -o flag to define the output filename and compile your hello.fs source code like this:

现在,使用fsharpc命令,使用-o标志定义输出文件名,并像下面这样编译hello.fs源代码:

  • fsharpc hello.fs -o hello

    fsharpc hello.fs -o你好

The preceding command will generate a hello executable file you can run with the mono command:

前面的命令将生成一个hello可执行文件,您可以使用mono命令运行该文件:

  • mono hello

    单声道你好

This gives you the following output and awaits user input:

这将为您提供以下输出并等待用户输入:


   
   
Output
Who are you?

If you type in Sammy, you will get the following.

如果输入Sammy ,则会得到以下内容。


   
   
Output
Oh, Hello Sammy! I'm F#.

Press a final keystroke, and the program will end.

按下最后一个按键,程序将结束。

Congratulations! You have written and compiled your first F# program, both with Mono and .NET Core.

恭喜你! 您已经使用Mono和.NET Core编写并编译了第一个F#程序。

结论 (Conclusion)

In this tutorial, you installed tooling for F# programming, covering both .NET Core and Mono environments. You also tested examples of F# code and built executables. These are the first steps toward learning this practical functional language.

在本教程中,您安装了适用于F#编程的工具,涵盖了.NET Core和Mono环境。 您还测试了F#代码示例并构建了可执行文件。 这些是学习这种实用功能语言的第一步。

Next steps could be to learn the language and get in touch with the community. Also, with projects getting more complex, you might need to manage code and resources more efficiently. Package managers like NuGet or Paket are bridges to the strong ecosystem built around .NET and tools-of-choice for organizing large programs.

下一步可能是学习 语言并与社区取得联系。 此外,随着项目变得越来越复杂,您可能需要更有效地管理代码和资源。 诸如NuGetPaket之类的软件包管理器是围绕.NET和用于组织大型程序的首选工具构建的强大生态系统的桥梁。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-f-and-set-up-a-local-programming-environment-on-ubuntu-18-04

 类似资料: