F# Cheatsheet

let s, f, d, bi = 4.14F, 4.14, 0.7833M, 9999I

Pattern Matching Pattern matching is often facilitated through match keyword.

This cheatsheet glances over some of the common syntax of F# 3.0. If you have any comments, corrections, or suggested additions, please open an issue or send a pull request to https://github.com/dungpa/fsharp-cheatsheet.

val val val val

Comments

See Literals (MSDN) for complete reference.

Block comments are placed between (* and *). Line comments start from // and continue until the end of the line.

Functions

(* This is block comment *) // And this is line comment

s : float32 = 4.14f f : float = 4.14 d : decimal = 0.7833M bi : System.Numerics.BigInteger = 9999

The let keyword also defines named functions. let negate x = x * -1 let square x = x * x let print x = printfn "The number is: %d" x

XML doc comments come after /// allowing us to use XML tags to generate documentation.

let squareNegateThenPrint x = print (negate (square x))

/// The ‘let‘ keyword defines an (immutable) value let result = 1 + 1 = 2

Pipe and composition operators

Strings F# string type is an alias for System.String type. /// Create a string using string concatenation let hello = "Hello" + " World" Use verbatim strings preceded by @ symbol to avoid escaping control characters (except escaping " by ""). let verbatimXml = @"" We don’t even have to escape " with triple-quoted strings. let tripleXml = """""" Backslash strings indent string contents by stripping leading spaces. 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 Most numeric types have associated suffixes, e.g., uy for unsigned 8-bit integers and L for signed 64-bit integer. let b, i, l = 86uy, 86, 86L val b : byte = 86uy val i : int = 86 val l : int64 = 86L Other common examples are F or f for 32-bit floating-point numbers, M or m for decimals, and I for big integers.

let rec fib n = match n with | 0 -> 0 | 1 -> 1 | _ -> fib (n - 1) + fib (n - 2) In order to match sophisticated inputs, one can use when to create filters or guards on patterns: let sign x = match x with | 0 -> 0 | x when x < 0 -> -1 | x -> 1 Pattern matching can be done directly on arguments:

Pipe operator |> is used to chain functions and arguments together. Double-backtick identifiers are handy to improve readability especially in unit testing: let ‘‘square, negate, then print‘‘ x = x |> square |> negate |> print This operator is essential in assisting the F# type checker by providing type information before use: let sumOfLengths (xs : string []) = xs |> Array.map (fun s -> s.Length) |> Array.sum

let fst’ (x, _) = x or implicitly via function keyword: /// Similar to ‘fib‘; using ‘function‘ for pattern matching let rec fib’ = function | 0 -> 0 | 1 -> 1 | n -> fib’ (n - 1) + fib’ (n - 2) For more complete reference visit Pattern Matching (MSDN).

Collections Lists A list is an immutable collection of elements of the same type.

Composition operator » is used to compose functions: let squareNegateThenPrint’ = square >> negate >> print

Recursive functions The rec keyword is used together with the let keyword to define a recursive function: let rec fact x = if x < 1 then 1 else x * fact (x - 1) Mutually recursive functions (those functions which call each other) are indicated by and keyword: let rec even x = if x = 0 then true else odd (x - 1)

// Lists use square brackets and ‘;‘ delimiter let list1 = [ "a"; "b" ] // :: is prepending let list2 = "c" :: list1 // @ is concat let list3 = list1 @ list2 // Recursion on list using (::) operator let rec sum list = match list with | [] -> 0 | x :: xs -> x + sum xs

Arrays Arrays are fixed-size, zero-based, mutable collections of consecutive data elements. // Arrays use square brackets with bar let array1 = [| "a"; "b" |] // Indexed access using dot let first = array1.[0]

and odd x = if x = 1 then true else even (x - 1)

1

Sequences

Tuples and Records

A sequence is a logical series of elements of the same type. Individual sequence elements are computed only as required, so a sequence can provide better performance than a list in situations in which not all the elements are used.

A tuple is a grouping of unnamed but ordered values, possibly of different types:

// Sequences can use yield and contain subsequences let seq1 = seq { // "yield" adds one element yield 1 yield 2 // "yield!" adds a whole subsequence yield! [5..10] }

// Tuple construction let x = (1, "Hello")

F# Core has a few built-in discriminated unions for error handling, e.g., Option and Choice.

// Triple let y = ("one", "two", "three") // Tuple deconstruction / pattern let (a’, b’) = x The first and second elements of a tuple can be obtained using fst, snd, or pattern matching:

Higher-order functions on collections

• Using range operator .. let xs = [ 1..2..9 ] • Using list or array comprehensions let ys = [| for i in 0..4 -> 2 * i + 1 |]

let print’ tuple = match tuple with | (a, b) -> printfn "Pair %A %A" a b Records represent simple aggregates of named values, optionally with members: // Declare a record type type Person = { Name : string; Age : int }

• Using init function let zs = List.init 5 (fun i -> 2 * i + 1) Lists and arrays have comprehensive sets of higher-order functions for manipulation. • fold starts from the left of the list (or array) and foldBack goes in the opposite direction let xs’ = Array.fold (fun str n -> sprintf "%s,%i" str n) "" [| 0..9 |]

// Create a value via record expression let paul = { Name = "Paul"; Age = 28 } // ’Copy and update’ record expression let paulsTwin = { paul with Name = "Jim" } Records can be augmented with properties and methods: type Person with member x.Info = (x.Name, x.Age)

• reduce doesn’t require an initial accumulator let last xs = List.reduce (fun acc x -> x) xs • map transforms every element of the list (or array) let ys’ = Array.map (fun x -> x * x) [| 0..9 |] • iterate through a list and produce side effects let _ = List.iter (printfn "%i") [ 0..9 ] All these operations are also available for sequences. The added benefits of sequences are laziness and uniform treatment of all collections implementing IEnumerable<’T>. let zs’ = seq { for i in 0..9 do printfn "Adding %d" i yield i }

let optionPatternMatch input = match input with | Some i -> printfn "input is an int=%d" i | None -> printfn "input is missing" Single-case discriminated unions are often used to create type-safe abstractions with pattern matching support: type OrderId = Order of string

let c’ = fst (1, 2) let d’ = snd (1, 2)

The same list [ 1; 3; 5; 7; 9 ] or array [| 1; 3; 5; 7; 9 |] can be generated in various ways.

let rec depth = function | Node(l, _, r) -> 1 + max (depth l) (depth r) | Leaf -> 0

Records are essentially sealed classes with extra topping: default immutability, structural equality, and pattern matching support. let isPaul person = match person with | { Name = "Paul" } -> true | _ -> false

Discriminated Unions Discriminated unions (DU) provide support for values that can be one of a number of named cases, each possibly with different values and types. type Tree<’T> = | Node of Tree<’T> * ’T * Tree<’T> | Leaf

2

// Create a DU value let orderId = Order "12" // Use pattern matching to deconstruct single-case DU let (Order id) = orderId

Exceptions The failwith function throws an exception of type Exception. let divideFailwith x y = if y = 0 then failwith "Divisor cannot be zero." else x / y Exception handling is done via try/with expressions. let divide x y = try Some (x / y) with :? System.DivideByZeroException -> printfn "Division by zero!" None The try/finally expression enables you to execute clean-up code even if a block of code throws an exception. Here’s an example which also defines custom exceptions. exception InnerError of string exception OuterError of string let handleErrors x y = try try if x = y then raise (InnerError("inner")) else raise (OuterError("outer")) with InnerError(str) -> printfn "Error1 %s" str finally printfn "Always print this."

Classes and Inheritance

Interfaces and Object Expressions

Parameterized active patterns:

This example is a basic class with (1) local let bindings, (2) properties, (3) methods, and (4) static members.

Declare IVector interface and implement it in Vector’.

let (|DivisibleBy|_|) by n = if n % by = 0 then Some DivisibleBy else None

type Vector(x : float, y : float) = let mag = sqrt(x * x + y * y) // (1) member this.X = x // (2) member this.Y = y member this.Mag = mag member this.Scale(s) = // (3) Vector(x * s, y * s) static member (+) (a : Vector, b : Vector) = // (4) Vector(a.X + b.X, a.Y + b.Y)

type IVector = abstract Scale : float -> IVector type Vector’(x, y) = interface IVector with member __.Scale(s) = Vector’(x * s, y * s) :> IVector member __.X = x member __.Y = y

let fizzBuzz = function | DivisibleBy 3 & DivisibleBy 5 -> "FizzBuzz" | DivisibleBy 3 -> "Fizz" | DivisibleBy 5 -> "Buzz" | i -> string i Partial active patterns share the syntax of parameterized patterns but their active recognizers accept only one argument.

Another way of implementing interfaces is to use object expressions.

Compiler Directives #load "../lib/StringParsing.fs"

type Animal() = member __.Rest() = ()

type ICustomer = abstract Name : string abstract Age : int

type Dog() = inherit Animal() member __.Run() = base.Rest()

let createCustomer name age = { new ICustomer with member __.Name = name member __.Age = age }

Upcasting is denoted by :> operator.

Active Patterns

Call a base class from a derived one.

Load another F# source file into FSI.

Reference a .NET assembly (/ symbol is recommended for Mono compatibility). #r "../lib/FSharp.Markdown.dll" Include a directory in assembly search paths. #I "../lib" #r "FSharp.Markdown.dll"

Complete active patterns: let dog = Dog() let animal = dog :> Animal Dynamic downcasting (:?>) might throw an InvalidCastException if the cast doesn’t succeed at runtime. let shouldBeADog = animal :?> Dog

let (|Even|Odd|) i = if i % 2 = 0 then Even else Odd

Other important directives are conditional execution in FSI (INTERACTIVE) and querying current directory (__SOURCE_DIRECTORY__).

let testNumber i = match i with | Even -> printfn "%d is even" i | Odd -> printfn "%d is odd" i

#if INTERACTIVE let path = __SOURCE_DIRECTORY__ + "../lib" #else let path = "../../../lib" #endif

3

F# Cheatsheet - GitHub

Mutually recursive functions (those functions which call each other) are indicated by ... The same list [ 1; 3; 5; 7; 9 ] or array [| 1; 3; 5; 7; 9. |] can be generated in ...

192KB Sizes 35 Downloads 300 Views

Recommend Documents

Paredit Cheatsheet - GitHub
Go to the start of current/previous defun, go to the start of next defun. Leader

Value category cheatsheet - GitHub
Any function call returning a non-reference value type, including pointers, yields a prvalue. ... A non-static data member of an lvalue is also an lvalue. int &&a{ 77 }; ...

the accessibility cheatsheet - GitHub
Jun 2, 2015 - 2 - “Operable” - User interface components and navigation must be ... Lay out your HTML the way the page is inteaded to be read and, where ...

HotSpot JVM options cheatsheet - GitHub
G1 is not deterministic, so no guaranties for GC pause to satisfy this limit. -XX:G1ReservePercent=10 Percentage of heap to keep free. Reserved memory is used ...

JJ-7:f( .. f - GitHub
B.E.AIIHXOBCKHH, H.H.CHJIHH. KAHAJI BBO~A-BhiBO~A EC 38M HA B3CM-6. (OBUll1E ITPHHIJJ10hl). JJ-7:f( .. f--(. Pll - 10088 ...

F-Statistics - GitHub
Page 1. F-Statistics. Nora Mitchell. January 31, 2017. Page 2 ... Work through the Jombart (2015) tutorial parts 1, 2.1, 3, 4.1, 4.2, and 5 on your own / with partner ...

Python Debugger Cheatsheet
Author: Florian Preinstorfer ([email protected]) — version 1.1 — license cc-by-nc-sa 3.0. See https://github.com/nblock/pdb-cheatsheet for more information.

André F. Rendeiro – Curriculum Vitae - GitHub
chine learning; Software development. Web development. Flask/Django, Javascript. Molecular Biology. Techniques Chromatin imunoprecipitation (ChIP), library ...

markdown-cheatsheet-online.pdf
would otherwise have special meaning in Markdown's formaing syntax. Markdown provides backslash escapes for. the following characters: \ backslash. ` backtick. * asterisk. _ underscore. {} curly braces. [] square brackets. () parentheses. # hash mark

markdown-cheatsheet-online.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Main menu.

Low-Voltage Motor Driver With Serial Interface .. (Rev. F) - GitHub
5. Logic. DAC. DRV8830. SLVSAB2F –MAY 2010–REVISED FEBRUARY 2012 ... (2) All voltage values are with respect to network ground terminal. ...... to any combination, machine, or process in which TI components or services are used.

Masters cheatsheet - top 80.pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Masters cheatsheet - top 80.pdf. Masters cheatsheet - top 80.pdf.

e f e f e f e f e f e f e f e f e f e f e f e f e f e
With your bitter, twisted lies,. You may trod me in the very dirt. But still, like dust, I'll rise. Does my sassiness upset you? Why are you beset with gloom? 'Cause I walk like I've got oil wells. Pumping in my living room. Just like moons and like

AC F F F F 2 F F θ F 2 F F (Dot Product) ACAB ⋅ F ACAB ⋅ = a1a2 + ...
AC. F F F F 2 F F θ. F 2 F. F (Dot Product) ACAB. ⋅. = AB AC cosθ. F. ACAB. ⋅. = a1a2 +b1b2 a1,b1 a2,b2. F AB. AC. ACAB. ⋅. = a1a2 +b1b2. F F. ACAB. ⋅.

Diablo-3-cheatsheet-62-63.pdf
Doom Hammer. Dread Lance. Exorcist. Guru Staff. Heaven Hand. Hellion Crossbow. High Scabbard. Razorspikes. Revenant Bow. Rune Sword. Sacred Shield.

F ก F ก F ก
ก (Transaction Processing System: TPS). 2. ก ก. (Management Reporting System: MRS). 3. ก (Decision Support. Systems: DSS). 4. ... ก F(Customer). F (Supplier).

Diablo-3-cheatsheet-62-63.pdf
Grand Chain. Grand Halberd. Heaven Strand. Impellor. Kurastian Asp. Massacre Axe. Mythical Staff. Oni Blade. Pallium. Phantom Bow. Piercer. Ring. Sagaris. Sanctified. Quiver. Slag Hammer. Sovereign. Greaves. Sovereign Helm. Sovereign Mail. Sovereign.

l -f*"
6. rovide the teathers a forum to share best practices ard address common concerns and problems in teaching and teaz.litt development. The invited participants ...

f"E
From. Re. STATUS OF IMPLEMENTATION OF THE SCHOOL REPORT CARD ... C). 0 Q. CD. < -. 3 C. 9-. 0 CO. CD a. CD. CL M. ooCO. 0-. 0 a. 0. 0. 0. 0-. CC-. -•.

'F in
Mar 5, 1992 - the Q output of ?ip-?op 220 is provided to the D input of ?ip-?op 222, and the next rising edge of the pulse from oscillator 216 Will cause the not ...

'F in
Mar 5, 1992 - BRIEF DESCRIPTION OF THE DRAWINGS. FIG. 1 is an electrical .... With the template signal, the product Will folloW this characteristic, and ...

08/05/15 11:04 f=0.49 /Users/rene/dev/stmbl/hw/eagle/IRAMX ... - GitHub
Aug 5, 2015 - PC0. 8. PC1. 9. PC2. 10. PC3. 11. PC4. 24. PC5. 25. PC6. 37. PC7. 38. PC8. 39. PC9. 40. PC10. 51. PC11. 52. PC12. 53. PD2. 54. VBAT. 1. C4.

¿m= If: f(x) = max f(x), f £ A, n £ N I.
real valued functions defined on a compact metric space X contains the con- ... the closure under minima and maxima: Am = \f: f(x) = min f(x), f£A, n £ N i ,. 1.

GitHub
domain = meq.domain(10,20,0,10); cells = meq.cells(domain,num_freq=200, num_time=100); ...... This is now contaminator-free. – Observe the ghosts. Optional ...