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:
- Immutable Data Structures: Lists, arrays, maps, sets, and other collections are immutable by default, promoting safer concurrent programming and predictable state management.
- Discriminated Unions: A powerful way to model types that can have one of several distinct forms, enabling expressive pattern matching.
- Pattern Matching: A core F# feature that allows for elegant destructuring and conditional logic based on the shape of data.
- Active Patterns: User-defined patterns that extend the power of pattern matching beyond simple structural checks.
- Sequence Expressions: A syntactic construct for building and manipulating sequences lazily, ideal for asynchronous operations and complex data pipelines.
- Computation Expressions: A generalized form of sequence expressions, enabling custom workflows for tasks like asynchronous programming, error handling, and mutable state management.
- Built-in Functions: A comprehensive set of utility functions for common operations like mapping, filtering, folding, and string manipulation.
Common Modules and Types
Here are some of the most frequently used modules and types within FSharp.Core:
-
List<T>Represents an immutable singly-linked list. Operations are O(1) at the head. -
ArrayModule providing functions for working with mutable arrays. -
SeqModule providing functions for working with sequences (lazy, potentially infinite collections). -
Map<Key, Value>Represents an immutable mapping from keys to values. -
Set<T>Represents an immutable set of unique elements. -
Result<T, TError>A type that represents either a success value or an error value, often used for explicit error handling. -
Option<T>A type that represents an optional value, which can be eitherSome(value)orNone. -
printf/sprintfFunctions for formatted string output, similar to C'sprintf. -
(| ... |)Active Patterns Syntax for defining custom pattern matching constructs.
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!