F#

本页使用了标题手工转换
本页使用了标题或全文手工转换,现处于中国大陆简体模式
求闻百科,共笔求闻
F#
编程范型多范型: 函数式, 指令式, 面向对象, 元编程, 并发计算
设计者微软研究院, Don Syme
实现者微软, F♯软件基金会
发行时间2005年 (2005) (version 1.0)
类型系统静态类型, 强类型, 类型推论
操作系统跨平台 (.NET框架, Mono, JavaScript)
许可证Apache许可证
文件扩展名.fs, .fsi, .fsx, .fsscript
网站fsharp.org
受启发于
ML, OCaml, C#, Python, Haskell,[1]Scala, Erlang
施影响于
F*, LiveScript

F#是由微软发展的为.NET语言提供运行环境的程序设计语言,是函数编程语言FP,Functional Programming),函数编程语言最重要的基础是Lambda Calculus。它是基于OCaml的,而OCaml是基于ML函数编程语言。有时F#和OCaml的程序是可以交互编译的。

F#支持高阶函数、柯里化惰性求值、Continuations、模式匹配、闭包、列表处理和元编程。这是一个用于显示.NET在不同编程语言间互通的程序设计,可以被.NET中的任意其它代码编译和调用。

2002年微软开始由Don Syme带领研发F#,从C#、LINQHaskell中获取了经验,2005年推出第一个版本,2007年7月31日释出1.9.2.9版。2007年底,微软宣布F#进入产品化的阶段。

F#已被集成在Visual Studio 2010中,版本是2.0,含有对.Net Framework的完全支持。

F#现在在Visual Studio 2015中,版本是4.0。

F#现在在Visual Studio 2017中,版本是4.1。

范例

一些小小范例如下:

/// 这是一个注释示例
printfn "Hello World!"

具有构造函数的Person类,该构造函数具有名称和年龄以及两个不可变属性。

/// 类型定义
type Person(name : string, age : int) =
    member x.Name = name
    member x.Age = age
    
/// 类的实例化
let mrSmith = Person("Smith", 42)

此处以32位的阶乘函数为例,演示F#的函数语法:

/// 采用模式匹配函数
let rec factorial n =
    match n with
    | 0 -> 1
    | _ -> n * factorial (n - 1)

/// 对于单参数函数,我们可以采用模式匹配函数
let rec factorial = function 
    | 0 -> 1 
    | n -> n * factorial (n - 1)
    
/// Using fold and range operator
let factorial n = [1..n] |> Seq.fold (*) 1

迭代示例:

/// 使用for循环进行迭代
let printList lst = 
    for x in lst do
        printfn "%d" x

/// 使用高阶函数进行迭代
let printList2 lst = 
    List.iter (printfn "%d") lst

/// 使用递归和模式匹配进行迭代
let rec printList3 lst =
    match lst with
    | [] -> ()
    | h :: t ->
        printfn "%d" h
        printList3 t

斐波那契数列数列示例:

/// 斐波那契数列公式
let fib n =
    let rec g n f0 f1 =
        match n with
        | 0 -> f0
        | 1 -> f1
        | _ -> g (n - 1) f1 (f0 + f1)
    g n 0 1

/// Another approach - a lazy infinite sequence of Fibonacci numbers
let fibSeq = Seq.unfold (fun (a,b) -> Some(a+b, (b, a+b))) (0,1)

// 打印斐波那契数列中的偶数
[1 .. 10]
|> List.map     fib
|> List.filter  (fun n -> (n % 2) = 0)
|> printList

// 同上,采用列表表达式
[ for i in 1..10 do
    let r = fib i
    if r % 2 = 0 then yield r ]
|> printList

一个Windows程序样本示例:

// 打开Windows窗口库
open System.Windows.Forms

// 创建窗口,并设置一些属性
let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#")

// 创建标签并显示文字
let label =
    let x = 3 + (4 * 5)
    new Label(Text = sprintf "x = %d" x)

// 将标签添加至窗口
form.Controls.Add(label)

// 运行窗口
[<System.STAThread>]
Application.Run(form)

多线程编程示例(此处为CPU和I/O任务同时进行):

/// 一个简易的素数判别程序
let isPrime (n:int) =
   let bound = int (sqrt (float n))
   seq {2 .. bound} |> Seq.forall (fun x -> n % x <> 0)

// 采用异步
let primeAsync n =
    async { return (n, isPrime n) }

/// 使用多线程计算m到n之间的所有素数
let primes m n =
    seq {m .. n}
        |> Seq.map primeAsync
        |> Async.Parallel
        |> Async.RunSynchronously
        |> Array.filter snd
        |> Array.map fst

// 测试函数
primes 1000000 1002000
    |> Array.iter (printfn "%d")

参考文献

  1. Syme, Granicz & Cisternino (2007:2页) "F# also draws from Haskell particularly with regard to two advanced language features called sequence expressions and workflows."

外部链接