fbpx
维基百科

F#

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

F#
编程范型多范型: 函数式, 指令式, 面向对象, 元编程, 并发计算
設計者微软研究院, Don Syme英语Don Syme
實作者微软, F♯软件基金会英语F Sharp Software Foundation
发行时间2005年 (2005) (version 1.0)
当前版本
  • 7.0 (2022年11月8日;穩定版本)[1]
型態系統静态类型, 强类型, 类型推论
操作系统跨平台 (.NET, .NET框架, Mono, JavaScript)
許可證Apache许可证
文件扩展名.fs, .fsi, .fsx, .fsscript
網站fsharp.org
啟發語言
ML, OCaml, C#, Python, Haskell,[2] Scala, Erlang
影響語言
F*, LiveScript英语LiveScript

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。

范例 编辑

一些小小范例如下:

// 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# A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative 32-bit integers, here shown in 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. ^ 1.0 1.1 https://devblogs.microsoft.com/dotnet/announcing-fsharp-7/.
  2. ^ Syme, Granicz & Cisternino (2007:2頁) "F# also draws from Haskell particularly with regard to two advanced language features called sequence expressions and workflows."

外部链接 编辑

是由微软发展的为, net语言提供运行环境的程序设计语言, 是函數程式語言, functional, programming, 函數程式語言最重要的基礎是lambda, calculus, 它是基于ocaml的, 而ocaml是基于ml函数程式语言, 有時和ocaml的程式是可以交互編譯的, 编程范型多范型, 函数式, 指令式, 面向对象, 元编程, 并发计算設計者微软研究院, syme, 英语, syme, 實作者微软, 软件基金会, 英语, sharp, software, foundation, 发行时间20. F 是由微软发展的为 NET语言提供运行环境的程序设计语言 是函數程式語言 FP Functional Programming 函數程式語言最重要的基礎是Lambda Calculus 它是基于OCaml的 而OCaml是基于ML函数程式语言 有時F 和OCaml的程式是可以交互編譯的 F 编程范型多范型 函数式 指令式 面向对象 元编程 并发计算設計者微软研究院 Don Syme 英语 Don Syme 實作者微软 F 软件基金会 英语 F Sharp Software Foundation 发行时间2005年 2005 version 1 0 当前版本7 0 2022年11月8日 穩定版本 1 型態系統静态类型 强类型 类型推论操作系统跨平台 NET NET框架 Mono JavaScript 許可證Apache许可证文件扩展名 fs fsi fsx fsscript網站fsharp wbr org啟發語言ML OCaml C Python Haskell 2 Scala Erlang影響語言F LiveScript 英语 LiveScript 維基教科書中有關F Sharp Programming的文本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 范例 编辑一些小小范例如下 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 A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non negative 32 bit integers here shown in F Using pattern matching expression let rec factorial n match n with 0 gt 1 gt n factorial n 1 For a single argument functions there is syntactic sugar pattern matching function let rec factorial function 0 gt 1 n gt n factorial n 1 Using fold and range operator let factorial n 1 n gt 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 gt h t gt printfn d h printList3 t 斐波那契数列数列示例 Fibonacci Number formula let fib n let rec g n f0 f1 match n with 0 gt f0 1 gt f1 gt 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 gt Some a b b a b 0 1 Print even fibs 1 10 gt List map fib gt List filter fun n gt n 2 0 gt printList Same thing using a list expression for i in 1 10 do let r fib i if r 2 0 then yield r gt 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 lt System STAThread gt Application Run form 多线程编程示例 此处为CPU和I O任务同时进行 A simple prime number detector let isPrime n int let bound int sqrt float n seq 2 bound gt Seq forall fun x gt n x lt gt 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 gt Seq map primeAsync gt Async Parallel gt Async RunSynchronously gt Array filter snd gt Array map fst Run a test primes 1000000 1002000 gt Array iter printfn d 参考文献 编辑 1 0 1 1 https devblogs microsoft com dotnet announcing fsharp 7 Syme Granicz amp Cisternino 2007 2頁 harv error no target CITEREFSymeGraniczCisternino2007 help F also draws from Haskell particularly with regard to two advanced language features called sequence expressions and workflows 外部链接 编辑微软F 页面存档备份 存于互联网档案馆 F Sharp Planet F 英文 F 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title F amp oldid 73481431, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。