F♯

出自維基百科,自由嘅百科全書
F#
編程範式多範型函數式程式命令式程式物件導向元程式並發程式
設計者微軟研究院Don Syme英文Don Syme
編程員微軟F♯軟件基金會英文F Sharp Software Foundation
第一次出現2005年 (2005)(version 1.0)
穩定版本
16.8[1] 喺維基數據度改 / 2020年11月10號,3年之前
類型系統靜態類型強類型類型推論
操作系統跨平台.NET框架MonoJavaScript
軟件授權Apache許可證
副檔名.fs, .fsi, .fsx, .fsscript
網站fsharp.org
啟發語言
MLOCamlC#PythonHaskell,[2] Scala, Erlang
影響語言
F*LiveScript英文LiveScript

F#係由微軟.NET語言提供運行環境而整出嚟嘅一隻程式語言,係函數程式語言FP,Functional Programming),函數程式語言最重要嘅基礎係Lambda Calculus。佢係基於OCaml嘅,而OCaml係基於ML嘅,所以有時F#同埋OCaml嘅程式可以交互編譯。

F#支援高階函數、柯里化惰性求值、Continuations、模式匹配、閉包、列表處理同埋元編程。呢個係一個用喺顯示.NET喺不同編程語言間互通嘅程序設計,可以畀.NET中嘅任意其它代碼編譯同埋調用。

2002年微軟開始由Don Syme帶領研發F#,由C#,LINQ同埋Haskell入面獲取嘅經驗,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。

應用領域[編輯]

F# 係一種 通用編程語言

網絡編程[編輯]

SAFE Stack 係用喺開發 Web 應用程序嘅P2P F# 堆棧。佢係伺服器端用 ASP.NET Core,喺客戶端用 Fable[3]

另一個P2P F# 選項係 WebSharper 框架。[4]

跨平台應用開發[編輯]

F# 可以同 Visual Studio Tools for Xamarin 一齊用,幫 iOS 同埋 Android 開發應用程序。[1] 庫提供咗更好睇嘅功能界面。

分析編程[編輯]

其中,F# 用於量化金融編程、[5] 能源交易同投資組合優化、[6] 機器學習,[7] 商業智能[8] 同埋 Facebook 上嘅社交遊戲。[9]

2010 年代,F# 被人定位做 C# 嘅優化替代品。F# 嘅腳本編寫能力同所有 Microsoft 產品嘅跨語言兼容性令佢喺開發人員入面廣受歡迎。[10]

腳本[編輯]

F# 可以用喺腳本語言,主要用喺桌面 read–eval–print loop(REPL)腳本。[11]

開源社區[編輯]

F# open-source 社區包括 F# Software Foundation[12] 同埋 GitHub 上的 F# Open Source Group。[12] 流行嘅開源 F# 項目包括:

  • [2],一個基於 Babel 嘅 F# 轉 Javascript 嘅轉譯器。
  • [3],.NET 嘅替代包管理器,仍然可以用 NuGet 存儲庫,但係具有集中嘅版本管理。
  • [4],一個 F# 友好嘅構建系統。
  • Giraffe,用於 ASP.NET Core 面向功能嘅中間件。
  • [5],一個輕量級嘅網絡伺服器同埋網絡開發庫。

兼容性[編輯]

F# 具有傳統嘅「ML 兼容模式」,可以粗略咁直接編譯用 OCaml 嘅大子集編寫嘅程序,冇函子、對象、多態變體或者其他添加。

例子[編輯]

下面係F#嘅Hello World:

 // 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位嘅階乘函數作為例子:

 /// 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. "Release 16.8". 2020年11月10號. 喺2023年3月19號搵到.
  2. Syme, Don; Granicz, Adam; Cisternino, Antonio (2007), Expert F#, Apress, p. 2
  3. Fable:你可以引以為豪嘅 JavaScript!. fable.io. 喺2017-12-09搵到.
  4. Intellifactory (2012-11-24). "WebSharper home".
  5. Microsoft 案例研究:Microsoft Visual Studio 2012 - 金融服務公司. Microsoft. 喺2012-11-25搵到.
  6. F# 用於能源交易和投資組合優化. 原著喺2015年9月1號歸檔. 喺2012-11-25搵到.
  7. Microsoft 案例研究:Grange 保險. Microsoft. 喺2012-11-25搵到.
  8. Trelford, Phil (2007). 使用 F# 學習. 四屆 ACM SIGPLAN 函數式編程商業用戶研討會論文集 - CUFP '07. Cufp '07. pp. 1–2. doi:10.1145/1362702.1362709. ISBN 9781450378444. S2CID 24018363. 喺2012-11-25搵到.{{cite book}}: CS1 maint: url-status (link)
  9. F# Facebook 社交遊戲工作. 喺2012-11-25搵到.
  10. "F# Developer Testimonials". 喺2021年5月25日搵到.
  11. F# 中嘅腳本. 喺2020-01-17搵到.
  12. 12.0 12.1 The F# Software Foundation. "F# Compiler (open source edition) @ github". 喺2012-11-24搵到.

出面網頁[編輯]