F Sharp
F#(エフ シャープ)はマイクロソフトが開発した.NET向けのマルチパラダイムプログラミング言語である。Visual Studio 2010…
|
F#のロゴ | |
| パラダイム |
関数型プログラミング、命令型プログラミング、オブジェクト指向プログラミング、メタプログラミング、リフレクション、並行計算 |
|---|---|
| 登場時期 | 2005年 |
| 開発者 |
マイクロソフト、F Sharp Software Foundation |
| 最新リリース |
10.0[1] |
| 型付け | 強い静的型付け |
| 影響を受けた言語 |
ML、OCaml、C Sharp、Erlang、Haskell、Python、Scala |
| 影響を与えた言語 | F* |
| プラットフォーム |
クロスプラットフォーム |
| ライセンス |
Apache License |
| ウェブサイト |
fsharp |
| 拡張子 |
fs、fsi、fsx、fsscript |
F#(エフ シャープ)はマイクロソフトが開発した.NET向けのマルチパラダイムプログラミング言語である。Visual Studio 2010より標準開発言語として追加された。
概要
2002年からマイクロソフトリサーチのドン・サイム (英: Don Syme) ら [2] によってOCamlをベースに開発が始められた。
OCamlから多くの要素を引き継いだ関数型とオブジェクト指向のマルチパラダイムである[3]。型安全であり[3]、型推論の機能をもつ[4]。ただし、オーバーロードをサポートしているため、OCamlのもつ型推論の完全性を失っている。C#やVisual Basic .NETなどの.NET言語と相互運用性があり[5]、.NETクラスライブラリの利用・開発が可能であり[6]、MonoおよびXamarinを利用したAndroidアプリケーション開発もサポートされている[7]。以前はSilverlightを利用したWindows Phone 7のアプリケーション開発もサポートされていた。
F#のFはFunctional programming language(関数型プログラミング言語)およびSystem Fが由来 [8]である。
F#の開発環境はVisual Studioの有償版製品(あるいは無償のCommunityエディション)にVisual F#として含まれているほか、Expressエディションで利用可能な無償ツールの配布もされている[9][10][11][12]。Visual F# Tools 4.1でRoslynをサポートするようになった。また、Monoや.NET Core[13][14]環境向けにもF#コンパイラが移植されているため、macOSやLinuxなどでもF#プログラムの開発および実行ができる。
OCaml互換の標準ライブラリを備えており、F#とOCamlのどちらでもコンパイルできるコードを記述することも可能である。しかしクラスの構文などはF#とOCamlで異なっている。
構文
OCamlと互換性のある冗語構文 (英: verbose syntax) と、Pythonのようなインデント(オフサイドルール)による軽量構文 (英: lightweight syntax) の二種類の構文を利用できる。標準では軽量構文が有効になっている。
例
Hello world
(* これはコメント *)
// 1行コメント。
(* Hello world プログラム *)
printfn "Hello World!"
let rec factorial n =
match n with
| 0 -> 1
| _ -> n * factorial (n - 1)
再帰関数の例
(* int リストの要素を再帰的にプリントする *)
let rec printList lst =
match lst with
| [] -> ()
| h :: t ->
printf "%d\n" h
printList t
(* 上と同様だが任意の型の要素をプリントする *)
let rec printList2 l =
match l with
| [] -> ()
| h :: t -> printfn "%A" h
printList2 t
(* match の代りに function 式を利用する *)
let rec printList3 = function
| [] -> ()
| h :: t -> printfn "%A" h
printList3 t
(* 高階関数を利用する *)
let printlist4 lst = List.iter (printfn "%A") lst
(* フィボナッチ数列 *)
let rec fib n =
match n with
| 0 | 1 -> n
| _ -> fib (n - 1) + fib (n - 2)
(* 遅延再帰シーケンス式によるフィボナッチ数列 *)
let rec fibs = Seq.cache <| seq { yield! [1; 1]
for x, y in Seq.zip fibs <| Seq.skip 1 fibs -> x + y }
(* 遅延無限シーケンスによるフィボナッチ数列 *)
let fibSeq = Seq.unfold (fun (a,b) -> Some(a+b, (b, a+b))) (1,1)
(* 偶数のフィボナッチ数をプリントする *)
[1 .. 10]
|> List.map fib
|> List.filter (fun n -> (n % 2) = 0)
|> printList
(* 同じことをシーケンス式を利用する *)
[ for i in 1..10 do
let r = fib i
if r % 2 = 0 then yield r ]
|> printList
Windows フォームを使用した例
(* フォームの作成 *)
open System.Windows.Forms
let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#")
(* フォーム テキストを決める *)
let x = 3 + (4 * 5)
do form.Text <- (if x = 23 then "Correct!" else "incorrect")
F#で書かれたソフトウェア
脚注
- ^ “What's new in F# 10” (2025年11月11日). 2026年7月24日閲覧。
- ^ “F# at Microsoft Research”. Microsoft. 2016年8月26日閲覧。
- ^ a b Chris Smith 2008, p. VII, まえがき.
- ^ Chris Smith 2008, p. 23, 2章 基本事項.
- ^ Chris Smith 2008, p. 383, 付録 B F#の相互運用性.
- ^ Chris Smith 2008, p. 8, 1章 F#の紹介.
- ^ “Xamarin.Android - Xamarin”. Microsoft. 2019年9月15日閲覧。
- ^ “The A-Z of programming languages: F#”. en:Network World by IDG. 2016年8月26日閲覧。
- ^ “Download Microsoft F#, August 2010 Community Technology Preview from Official Microsoft Download Center”. Microsoft. 2015年5月28日閲覧。[リンク切れ]
- ^ “Download F# Tools for Visual Studio Express 2012 for Web from Official Microsoft Download Center”. Microsoft. 2015年5月28日閲覧。
- ^ “Announcing Visual F# Tools 3.1.1, with support for Desktop and Web express - Visual F# Tools Team Blog - Site Home - MSDN Blogs”. Microsoft. 2015年5月28日閲覧。
- ^ “Announcing the release of Visual F# Tools 3.1.2 - Visual F# Tools Team Blog - Site Home - MSDN Blogs”. Microsoft. 2015年5月28日閲覧。
- ^ “Use F# on Linux”. F# Software Foundation. 2019年9月15日閲覧。
- ^ “Get started with F# with command-line tools”. Microsoft. 2019年9月15日閲覧。
参考文献
- Chris Smith 著、鈴木幸敏 訳、頃末和義 編『プログラミングF#』(初版)オライリー・ジャパン、2008年8月。ISBN 978-4-87311-466-8。
関連項目
外部リンク
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.