The rec keyword is used together with the let keyword to define a recursive function: let rec fact x = if x 0 Length)Ĭomposition operator > is used to compose functions: let squareNegateThenPrint' = square > negate > print Recursive functions
This operator is essential in assisting the F# type checker by providing type information before use: let sumOfLengths ( xs : string ) = xs |> Array. Double-backtick identifiers are handy to improve readability especially in unit testing: let ``square, negate, then print`` x = x |> square |> negate |> print Pipe operator |> is used to chain functions and arguments together. let negate x = x * - 1 let square x = x * x let print x = printfn "The number is: %d " x let squareNegateThenPrint x = print ( negate ( square x)) The let keyword also defines named functions. See Literals (MSDN) for complete reference. Other common examples are F or f for 32-bit floating-point numbers, M or m for decimals, and I for big integers. let b, i, l = 86uy, 86, 86L val b : byte = 86uy val i : int = 86 val l : int64 = 86L Most numeric types have associated suffixes, e.g., uy for unsigned 8-bit integers and L for signed 64-bit integer. let poem = "The lesser world was daubed \n \ By a colorist of modest skill \n \ A master limned you in the finest inks \n \ And with a fresh-cut quill." Basic Types and Literals let tripleXml = """"""īackslash strings indent string contents by stripping leading spaces. let verbatimXml = don't even have to escape " with triple-quoted strings. Use verbatim strings preceded by symbol to avoid escaping control characters (except escaping " by ""). / Create a string using string concatenation let hello = "Hello" + " World" / The `let` keyword defines an (immutable) value let result = 1 + 1 = 2 Stringsį# string type is an alias for System.String type. XML doc comments come after /// allowing us to use XML tags to generate documentation. (* This is block comment *) // And this is line comment Line comments start from // and continue until the end of the line.
Commentsīlock comments are placed between (* and *). If you have any comments, corrections, or suggested additions, please open an issue or send a pull request to. This cheatsheet glances over some of the common syntax of F# 3.0.