한국   대만   중국   일본 
F? - 維基百科,自由?百科全書 跳去內容

F?

出自維基百科,自由?百科全書
F#
編程範式 多範型 函數式程式 命令式程式 物件導向 元程式 ?發程式
設計者 微軟?究院 Don Syme 英文 Don Syme
編程員 微軟 F?軟件基金會 英文 F Sharp Software Foundation
第一次出現 2005年  ( 2005 ) (version 1.0)
穩定版本
16.8 [1]   喺維基數據度改 / 2020年11月10號,3年之前
類型系統 靜態類型 ?類型 類型推論
操作系統 跨平台 .NET?架 Mono JavaScript
軟件授權 Apache許可證
副?名 .fs, .fsi, .fsx, .fsscript
網站 fsharp .org
?發語言
ML OCaml C# Python Haskell , [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. F able:?可以引以?豪? JavaScript! . fable.io . ? 2017-12-09 ?到 .
  4. Intellifactory (2012-11-24). "WebSharper home" .
  5. M icrosoft 案例?究:Microsoft Visual Studio 2012 - 金融服務公司 . Microsoft . ? 2012-11-25 ?到 .
  6. F # 用於能源交易和投資組合優化 . 原著 ?2015年9月1號歸? . ? 2012-11-25 ?到 .
  7. M icrosoft 案例?究: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 ?到 .

出面網頁 [ 編輯 ]