F♯

維基百科,自由的百科全書
(重新導向自F Sharp
跳至導覽 跳至搜尋
F#
File:F Sharp logo.svg
編程範型多範式: 函數式, 指令式, 物件導向, 元程式設計, 並行計算
設計者微軟研究院, Don Syme英語Don Syme
實作者微軟, F♯軟件基金會英語F Sharp Software Foundation
釋出時間2005年 (2005) (version 1.0)
目前版本
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    型態系統靜態型別, 強型別, 類型推論
    作業系統跨平台 (.NET, .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#支援高階函數柯里化惰性求值續體模式匹配閉包列表推導式元程式設計。這是一個用於顯示.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。

    範例[編輯]

    一些小小範例如下:

    // This is a comment for a sample hello world program.
    printfn "Hello World!"
    

    具有構造函數的Person類,該構造函數具有名稱和年齡以及兩個不可變的屬性。

    /// This is a documentation comment for a type definition.
    type Person(name : string, age : int) =
        member x.Name = name
        member x.Age = age
        
    /// class instantiation
    let mrSmith = Person("Smith", 42)
    

    一個經常用於演示函數式語言語法的簡單範例。此處以32位元非負整數的階乘函數為例,使用F#。

    /// Using pattern matching expression
    let rec factorial n =
        match n with
        | 0 -> 1
        | _ -> n * factorial (n - 1)
    
    /// For a single-argument functions there is syntactic sugar (pattern matching function):
    let rec factorial = function 
        | 0 -> 1 
        | n -> n * factorial (n - 1)
        
    /// Using fold and range operator
    let factorial n = [1..n] |> Seq.fold (*) 1
    

    迭代範例:

    /// Iteration using a 'for' loop
    let printList lst = 
        for x in lst do
            printfn "%d" x
    
    /// Iteration using a higher-order function
    let printList2 lst = 
        List.iter (printfn "%d") lst
    
    /// Iteration using a recursive function and pattern matching
    let rec printList3 lst =
        match lst with
        | [] -> ()
        | h :: t ->
            printfn "%d" h
            printList3 t
    

    斐波那契數列數列範例:

    /// Fibonacci Number formula
    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)
    
    // Print even fibs
    [1 .. 10]
    |> List.map     fib
    |> List.filter  (fun n -> (n % 2) = 0)
    |> printList
    
    // Same thing, using a list expression
    [ for i in 1..10 do
        let r = fib i
        if r % 2 = 0 then yield r ]
    |> printList
    

    一個Windows程式樣本範例:

    // Open the Windows Forms library
    open System.Windows.Forms
    
    // Create a window and set a few properties
    let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#")
    
    // Create a label to show some text in the form
    let label =
        let x = 3 + (4 * 5)
        new Label(Text = sprintf "x = %d" x)
    
    // Add the label to the form
    form.Controls.Add(label)
    
    // Finally, run the form
    [<System.STAThread>]
    Application.Run(form)
    

    多線程編程範例(此處為CPU和I/O任務同時進行):

    /// A simple prime number detector
    let isPrime (n:int) =
       let bound = int (sqrt (float n))
       seq {2 .. bound} |> Seq.forall (fun x -> n % x <> 0)
    
    // We are using async workflows
    let primeAsync n =
        async { return (n, isPrime n) }
    
    /// Return primes between m and n using multiple threads
    let primes m n =
        seq {m .. n}
            |> Seq.map primeAsync
            |> Async.Parallel
            |> Async.RunSynchronously
            |> Array.filter snd
            |> Array.map fst
    
    // Run a test
    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."

    外部連結[編輯]