vs2019 开始自己的第一个F#程序

这是针对于博客vs2019安装和使用教程(详细)的F#项目新建示例,代码比较简单,适合入门~


目录

一、安装F#环境

二、启动程序

三、编写小程序

四、运行结果

五、注意事项


一、安装F#环境

1.运行安装程序,点击“更多”下拉菜单中的“修改”

2.勾选“数据科学和分析应用程序”和“.NET桌面开发”(可选),点击“下载时安装”


二、启动程序

1.点击菜单栏-->文件-->新建-->项目,语言选择F#,选择控制台应用程序,下一步

2.选择项目位置,命名项目,之后点击确定

3.可以看到生成了两个.fs文件

                                                                              

4.两个文件内容如下

AssemblyInfo.fs

namespace fsharp_first_try.AssemblyInfo

open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[<assembly: AssemblyTitle("fsharp_first_try")>]
[<assembly: AssemblyDescription("")>]
[<assembly: AssemblyConfiguration("")>]
[<assembly: AssemblyCompany("")>]
[<assembly: AssemblyProduct("fsharp_first_try")>]
[<assembly: AssemblyCopyright("Copyright ©  2019")>]
[<assembly: AssemblyTrademark("")>]
[<assembly: AssemblyCulture("")>]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[<assembly: ComVisible(false)>]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[<assembly: Guid("9b4ab5cd-49d0-4416-a02f-29ec1447478d")>]

// Version information for an assembly consists of the following four values:
//
//       Major Version
//       Minor Version
//       Build Number
//       Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [<assembly: AssemblyVersion("1.0.*")>]
[<assembly: AssemblyVersion("1.0.0.0")>]
[<assembly: AssemblyFileVersion("1.0.0.0")>]

do
    ()

Program.fs

[<EntryPoint>]
let main argv = 
    printfn "%A" argv
    0 // return an integer exit code

三、编写小程序

1.编写一个小程序,主要是对分支语句(if-then-else)、for循环函数的基本用法进行简单介绍,代码中有注释说明,不再赘述。程序如下:

Program.fs

// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.

//函数名称为Subtract1,参数为number1和number2,返回类型为double
let Subtract1 number1 number2 : double = 
    if(number1>number2)then
        number1 - number2 //返回
    else
        number2 - number1 //返回

let Subtract2 number1 number2 : double = 
    let number3: double = 0.0
    if(number1>number2)then
        let number3 = number1 - number2
        printfn "number3_2 is:%A" number3
    else
        let number3 = number2 - number1
        printfn "number4_2 is:%A" number3
    number3 //返回



[<EntryPoint>] //特性标记的函数,必须是编译序列中最后一个文件中的最后一个声明
let main argv = 
    printfn "%A" argv
    //输出a+b的结果赋值给c
    let a = 1
    let b = 2
    let c = a + b
    printfn "c is:%A" c
    // 输出数字0到9
    let d = [0 .. 9]
    printfn "d is:%A" d
    // 输出浮点数和强制类型转换为整型的数
    let e: double = 6.9
    printfn "double e:%A" e
    printfn "double2int e is:%A" (int e)
    // 重新赋值为整型
    let e: int = 6
    printfn "int e is:%A" e
    // 减法函数1(if then else)
    let number3_1 = Subtract1 2.3 3.4
    printfn "number3_1 is:%A" number3_1
    let number4_1= Subtract1 5.4 3.2
    printfn "number4_1 is:%A" number4_1
    // 减法函数2(if then else)
    let number3_2 = Subtract2 2.3 3.4
  
    let number4_2 = Subtract2 5.4 3.2
    //for 循环
    let f = [for i in 0 .. 9 -> i]
    printfn "f is:%A" f
    //创建一个长度为10,元素都为0的数组
    let g = Array.create 10 0
    printfn "g is:%A" g
    //对每个数组中的元素进行赋值,值为其索引
    for i = 0 to g.Length - 1 
        do Array.set g i i
    printfn "顺序输出:"
    for i = 0 to g.Length - 1 
        do
            printfn "%A" (Array.get g i)
    printfn "逆序输出:"
    for i = g.Length - 1 downto 0
        do
            printfn "%A" (Array.get g i)
    0 // return an integer exit code



四、运行结果

执行代码:

                                                                           

点击上方绿色启动,运行程序。

其中for...to...顺序输出,for ...downto...逆序输出:

                                                          

是不是很简单呢~


五、注意事项

1.[<EntryPoint>] 特性标记的函数,必须是编译序列中最后一个文件中的最后一个声明:也就是说如果在这个特性标记的代码段后面再写代码则会报错,应该在前面写,例如两个减法函数Subtract1Subtract2

2.分支语句中let不能是最后一行

3.函数返回值类型必须和函数声明的类型一致,不要忘写


返回至原博客:vs2019安装和使用教程(详细)

发布了129 篇原创文章 · 获赞 1105 · 访问量 169万+

猜你喜欢

转载自blog.csdn.net/qq_36556893/article/details/102721010