F# Core Library

The fundamental library for F# development in .NET

Overview

The F# core library, often referred to as FSharp.Core, is the foundational set of types and functions that make up the F# programming language. It provides essential building blocks for functional programming, including immutable data structures, powerful pattern matching, discriminated unions, active patterns, and a rich set of higher-order functions for collection manipulation.

Understanding and leveraging the F# core library is crucial for writing idiomatic and efficient F# code. This documentation outlines its key components and provides guidance on how to use them effectively.

Key Components

The F# core library is vast, but some of its most impactful features include:

Common Modules and Types

Here are some of the most frequently used modules and types within FSharp.Core:

Usage Examples

Working with Lists


let numbers = [1; 2; 3; 4; 5]
let doubled = List.map (fun x -> x * 2) numbers
printfn "Doubled: %A" doubled // Output: Doubled: [2; 4; 6; 8; 10]

let sum = List.sum numbers
printfn "Sum: %d" sum // Output: Sum: 15
            

Using Discriminated Unions and Pattern Matching


type Shape =
    | Circle of radius: float
    | Rectangle of width: float * height: float
    | Point

let getArea shape =
    match shape with
    | Circle r -> System.Math.PI * r * r
    | Rectangle (w, h) -> w * h
    | Point -> 0.0

let circleArea = getArea (Circle 5.0)
let rectArea = getArea (Rectangle (4.0, 6.0))
let pointArea = getArea Point

printfn "Circle Area: %.2f" circleArea // Output: Circle Area: 78.54
printfn "Rectangle Area: %.2f" rectArea // Output: Rectangle Area: 24.00
printfn "Point Area: %.2f" pointArea // Output: Point Area: 0.00
            

Using Options for Nullability


let safeDivide x y =
    if y = 0 then None
    else Some (x / y)

let result1 = safeDivide 10 2
let result2 = safeDivide 10 0

match result1 with
| Some value -> printfn "Division result: %f" value // Output: Division result: 5.000000
| None -> printfn "Cannot divide by zero."

match result2 with
| Some value -> printfn "Division result: %f" value
| None -> printfn "Cannot divide by zero." // Output: Cannot divide by zero.
            

Further Resources

This documentation is a living guide. Stay updated with the latest F# releases and best practices!