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# 項目包括:
兼容性
[
編輯
]
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"
)
參考
[
編輯
]
出面網頁
[
編輯
]