F#이란?

F#은 간결하고 강력하고 성능이 좋은 코드를 작성하기 위한 범용 프로그래밍 언어입니다.

F#을 사용하면 프로그래밍의 세부 정보가 아닌 기본 문제에 대한 포커스가 다시 기본 정리되지 않은 자체 문서화 코드를 작성할 수 있습니다.

그것은 속도와 호환성을 손상시키지 않고이 작업을 수행 - 그것은 오픈 소스, 플랫폼 간 상호 운용성입니다.

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

// Defines a list of names
let names = [ "Peter"; "Julia"; "Xi" ]

// Defines a function that takes a name and produces a greeting.
let getGreeting name = $"Hello, {name}"

// Prints a greeting for each name!
names
|> List.map getGreeting
|> List.iter (fun greeting -> printfn $"{greeting}! Enjoy your F#")

F#에는 다음을 비롯한 다양한 기능이 있습니다.

  • 간단한 구문
  • 기본값으로 불변
  • 형식 유추 및 자동 일반화
  • 첫 번째 클래스 함수
  • 강력한 데이터 형식
  • 패턴 일치
  • 비동기 프로그래밍

전체 기능 집합은 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이 아니고 변경할 수 없으며 기본적으로 비교할 수 있으므로 사용하기가 매우 쉽습니다.

함수 및 패턴 일치를 사용한 정확성

F# 함수는 쉽게 정의할 수 있습니다. 패턴 일치결합하면 컴파일러에서 정확성을 적용하는 동작을 정의할 수 있습니다.

// Returns a WithdrawalResult
let withdrawMoney amount = // Implementation elided

let handleWithdrawal amount =
    let w = withdrawMoney amount

    // The F# compiler enforces accounting for each case!
    match w with
    | Success s -> printfn $"Successfully withdrew %f{s.Amount}"
    | InsufficientFunds f -> printfn $"Failed: balance is %f{f.Balance}"
    | CardExpired d -> printfn $"Failed: card expired on {d}"
    | UndisclosedFailure -> printfn "Failed: unknown :("

F# 함수는 또한 첫 번째 클래스입니다. 즉, 매개 변수로 전달되고 다른 함수에서 반환될 수 있습니다.

개체에 대한 작업을 정의하는 함수

F#은 데이터와 기능을 혼합해야 할 때 유용한 개체에 대한 모든 지원을 제공합니다. F# 멤버 및 함수를 정의하여 개체를 조작할 수 있습니다.

type Set<'T when 'T: comparison>(elements: seq<'T>) =
    member s.IsEmpty = // Implementation elided
    member s.Contains (value) =// Implementation elided
    member s.Add (value) = // Implementation elided
    // ...
    // Further Implementation elided
    // ...
    interface IEnumerable<'T>
    interface IReadOnlyCollection<'T>

module Set =
    let isEmpty (set: Set<'T>) = set.IsEmpty

    let contains element (set: Set<'T>) = set.Contains(element)

    let add value (set: Set<'T>) = set.Add(value)

F#에서는 개체를 조작할 함수의 형식으로 처리하는 코드를 작성하는 경우가 많습니다. 제네릭 인터페이스, 개체 식 및 멤버현명한 사용과 같은 기능은 더 큰 F# 프로그램에서 일반적입니다.

다음 단계

더 큰 F# 기능 집합에 대해 자세히 알아보려면 F# 둘러보기를 검사.