F#語言入門之什么是F#語言


F#是一種函數式編程語言,可以輕松編寫正確且可維護的代碼。

F#編程主要涉及定義類型推斷和自動泛化的類型和函數。 這使您可以將焦點保留在問題域上並操縱其數據,而不是編程的細節。

open System // Gets access to functionality in System namespace.

// Defines a function that takes a name and produces a greeting.
let getGreeting name =
    sprintf "Hello, %s! Isn't F# great?" name

[<EntryPoint>]
let main args =
    // Defines a list of names
    let names = [ "Don"; "Julia"; "Xi" ]

    // Prints a greeting for each name!
    names
    |> List.map getGreeting
    |> List.iter (fun greeting -> printfn "%s" greeting)

    0

 

F#有許多功能,包括:

 

  • 輕量級語法
  • 默認不變
  • 類型推斷和自動泛化
  • 一流的功能
  • 強大的數據類型
  • 模式匹配
  • 異步編程

豐富的數據類型

記錄和識別聯合等數據類型允許您表示復雜的數據和域。
// Group data with Records
type SuccessfulWithdrawal = {
    Amount: decimal
    Balance: decimal
}

type FailedWithdrawal = {
    Amount: decimal
    Balance: decimal
    IsOverdraft: bool
}

// Use discriminated unions to represent data of 1 or more forms
type WithdrawalResult =
    | Success of SuccessfulWithdrawal
    | InsufficientFunds of FailedWithdrawal
    | CardExpired of System.DateTime
    | UndisclosedFailure

F#記錄和區分聯合在默認情況下是非null,不可變和可比較的,使它們非常容易使用。完整教程閱讀http://nopapp.com/Blog/Article/FSharp-What-Is-FSharp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM