💻 Programming

F#

A functional-first language for the .NET platform

📖 Detailed Explanation

F# is a functional-first programming language for the .NET platform developed by Microsoft. Designed in 2005 by Don Syme, inspired by the ML-family language OCaml, it is characterized by combining the powerful features of functional programming with the rich libraries of the .NET ecosystem.

F#'s greatest strengths are powerful type inference and pattern matching. Because the compiler automatically infers most types, type safety is guaranteed even without type declarations, and pattern matching allows complex data structures to be handled concisely and clearly.

In F#, where immutability is the default, variables are unchangeable by default, so there are fewer side effects and concurrent programming is safe. Function composition through the pipe operator (|>) allows data transformation pipelines to be expressed intuitively, so data processing code is highly readable.

In practice, F# is actively used in quantitative analysis in finance, data science, web backends (Giraffe, Saturn), and domain-driven design (DDD). As of 2025, it fully supports cross-platform development with .NET 8, and integration with external data sources is very simple through Type Providers.

💻 Code Example

// 1. 기본 문법과 타입 추론
let greeting = "Hello, F#!"  // 타입 추론: string
let numbers = [1; 2; 3; 4; 5]  // 타입 추론: int list

// 2. 파이프 연산자와 함수형 처리
let result =
    numbers
    |> List.filter (fun x -> x % 2 = 0)  // 짝수만: [2; 4]
    |> List.map (fun x -> x * x)          // 제곱: [4; 16]
    |> List.sum                           // 합계: 20

printfn "짝수 제곱의 합: %d" result

// 3. 패턴 매칭과 구분된 유니온
type Shape =
    | Circle of radius: float
    | Rectangle of width: float * height: float
    | Triangle of baseLen: float * height: float

let calculateArea shape =
    match shape with
    | Circle r -> System.Math.PI * r * r
    | Rectangle (w, h) -> w * h
    | Triangle (b, h) -> 0.5 * b * h

let shapes = [Circle 5.0; Rectangle (3.0, 4.0); Triangle (6.0, 8.0)]
shapes |> List.iter (fun s -> printfn "면적: %.2f" (calculateArea s))

// 4. 비동기 워크플로우 (Async)
open System.Net.Http

let fetchDataAsync (url: string) = async {
    use client = new HttpClient()
    let! response = client.GetStringAsync(url) |> Async.AwaitTask
    return response.Length
}

// 병렬 실행 예시
let urls = ["https://example.com"; "https://fsharp.org"]
let results =
    urls
    |> List.map fetchDataAsync
    |> Async.Parallel
    |> Async.RunSynchronously

printfn "가져온 데이터 크기: %A" results

🗣️ Say It Like This in Practice

💬 In a Meeting
"If we implement this data pipeline in F#, the pipe operator lets us express the transformation steps clearly. Since immutability is the default, parallel processing can also be done safely, which is advantageous for performance optimization as well."
💬 In an Interview
"Using F#'s Discriminated Unions and pattern matching, you can express the domain model with the type system. By following the 'Make Illegal States Unrepresentable' principle, you can prevent invalid states at compile time."
💬 In a code review
"You're using the mutable keyword here, but in F# it's better to write with immutable data whenever possible. In this case, you can replace it with fold or scan and get the same result without side effects."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Ignoring Indentation Sensitivity

In F#, indentation is part of the syntax, as in Python. Use spaces instead of tabs, and keep the indentation level consistent. It is recommended to configure your IDE to automatically convert tabs to spaces.

❌
Writing F# in a C# Style

Overusing classes and mutable variables prevents you from taking advantage of F#'s strengths. Prefer records, union types, and pure functions, and use object-oriented features only when necessary.

✅
Eliminating null with the Option Type

In F#, the Option type (Some/None) is used instead of null. Used together with pattern matching, it completely prevents NullReferenceException, greatly improving code stability.

🔗 Related Terms

📚 Learn More