Getting Started with F#
F#下载地址:http://research.microsoft.com/fsharp/fsharp.aspx
F# 是函数式编程语言,它是一系统的绑定的标识符表达式。程序如下:
let x = 3 + (4 * 5)
let res = (if x = 23 then "correct" else "incorrect")
当这个程序执行的时候 x 被赋值23,res的值是字符串”correct”。你可以直接在 F# 交互界面运行程序,每个语句后边加上 “;;”。
> bin\fsi.exe
MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version ...
> let x = 3 + (4 * 5);;
val x : int
> let res = (if x = 23 then "correct" else "incorrect");;
val res : string
> res;;
val it : string = "correct"
每个表达式执行后会立即计算出结果。表达式隐式绑定到变量 it上。
F# 不是一个纯的函数式语言,因此表示式中可以包括其它一些内容如:I/O,图形输出等等。 下面程序是一个F#控制台程序:
let x = "Hello World";;
System.Console.WriteLine(x);;
将上边的程序保存到 hello.fs 可以使用如下命令编译 F#程序代码:
> fsc hello.fs
执行程序
> hello.exe
F#的代码也可以放到一个库里使用扩展名为 .ml 的文件,例如:hello.ml。
lib.fs 文件包含如下代码:
let myLibFunction() = System.Console.WriteLine("Hello World")
hello.fs 文件包含如下代码:
let main = Lib.myLibFunction()
main 不是一个函数,可是左边表示式计算的结果值。使用下边的命令编译:
> fsc -a lib.fs
> fsc -r lib.dll hello.fs
制作一个 lib.dll 库和可执行 hello.exe。 这些都是 .NET 程序集。 可以编译你的库和程序,产生一个 hello2.exe:
> fsc -o hello2.exe lib.fs hello.fs