0% found this document useful (0 votes)
54 views

F# Cheat Sheet

F# is a statically typed, multi-paradigm language where types are inferred. It supports functions, records, variants, and pattern matching. Functions are declared with "let" and can be partial applications or higher-order. Variants represent algebraic data types and are pattern matched using "match". Records are products with named fields accessed via dot notation or pattern matching.

Uploaded by

HowTo Hack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

F# Cheat Sheet

F# is a statically typed, multi-paradigm language where types are inferred. It supports functions, records, variants, and pattern matching. Functions are declared with "let" and can be partial applications or higher-order. Variants represent algebraic data types and are pattern matched using "match". Records are products with named fields accessed via dot notation or pattern matching.

Uploaded by

HowTo Hack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Musa Al-hassy https://github.

com/alhassy/FSharpCheatSheet September 17, 2019 Functions

F# Cheat Sheet A function is declared with the let keyword —variables are functions of zero arguments.
Function & varaible names must begin with a lowercase letter, and may use _ or ’.

Administrivia  Identifiers may have spaces and punctuation in them if they are enclosed in double-
backticks; but no unicode or dashes in-general.
F# is a strict, statically, and strongly typed, multi-paradigm, language where types are
inferred. It supports first-order functions and currying. let ‘‘this & that‘‘ = 2
Roughly,  Functions are like variables, but with arguments, so the same syntax applies.
F# ≈ OCaml + C#
 Single-line comments begin with //.
(* A curried function *) // Composition
 Multi-line comments are enclosed in (* · · · *). let f x y = x + y let sum9 = f 4 >> f 5
 Here’s an example of explicit type annotations.
(* Function application *) // Threading: x |> f ≈ f x
let x : int = 3
let result = f 10 (2 * 6) 1 |> f 4 |> fun x -> 2 //// ⇒ 2
let first (x : ’a) (y: ’b) : ’a = x
Recursive definitions are marked with the
 Being “strongly typed” means that F# does little to no coercions, casts, for you. (* Partial application *) rec keyword.
// 5 / 2.5 (* Crashes: 5 and 2.5 are different types *) let g x = f x 2 let rec fact n
float 5 / 2.5 = if n = 0
≈ 5.0 / 2.5 then 1
≈ 2.0 else n * fact (n - 1)

F#’s conversion functions are named by the type they convert to; akin to C casts. Here’s an example of a higher-order function & multiple local functions & an infix oper-
◦ E.g., int 23.1 and int "23" both yield the integer 23. ator & an anonymous function & the main method is parametricly polymorphic.
◦ string is then the traditional “to string” method.
let try_add (bop : ’a -> ’a -> ’a) (test : ’a -> bool)
(fallback : ’a) (x : ’a) (y : ’a)
Getting Started = (* (/@/) x y = x /@/ y *)
let (/@/) x y = bop x y
The F# REPL and compiler are named fsi/fsc on Windows and fsharpi/fsharpc on let wrap a = if test a then a else fallback
Mac/Linux. ( Running these in Emacs Shell stalls; use ansi-term instead! ) wrap x /@/ wrap y
Ubuntu sudo apt install mono-complete fsharp
Mac brew install mono 699 = try_add (+) (fun a -> a % 3 = 0) (666) (-1) 33
(* The anonymous function uses ‘=’ as Boolean equality. *)
Emacs Setup Example Source File
(use-package fsharp) module CheatSheet -2 = -2 % 3 (* /Remainder/ after dividing out 3s *)
(use-package ob-fsharp)
let myInt = 1972;; Top level and nested functions are declared in the same way; the final expression in a
The [<EntryPoint>] is necessary for using definition is the return value.
[<EntryPoint>]
fsharpc. let main argv
= printfn "%s" (string myInt) We also have the η-rule: (fun x -> f x) = f.
0
F# has extension methods, like C#. That is, types are “open” —as in Ruby.
In a terminal, one runs fsharpi CheatSheet.fs to load this script, then open
CheatSheet;; to have unqualified access to all contents; otherwise type in type System.String with
CheatSheet.myInt;; to access items. One may enter multiple lines in the REPL, then member this.IsCool = this.StartsWith "J"
execute them by entering ;;. Use #quit;; to leave the REPL.
// Try it out.
Execute fsharpc CheatSheet.fs; mono CheatSheet.exe to compile the file then run it. true = "Jasim".IsCool

1
Booleans Variants and Pattern Matching
Inequality is expressed with <>. Sums, or “variants”: A unified way to combine different types into a single type;
(* false, true, false, true, false, true, true, 1 *)  Essentially each case denotes a “state” along with some relevant “data”.
let x , y = true , false
in x = y, x || y, x && y, x >= y, 12 < 2, "abc" <= "abd"  Constructors must begin with a capital letter.
, 1 <> 2, if x then 1 elif y then 2 else 3
 We may parameterise using OCaml style, ’a, or/and C# style, <’a>.

Strings type ’a Expr = Undefined | Var of ’a | Const of int | Sum of Expr<’a> * ’a Expr

F# strings are not arrays, or lists, of characters as in C or Haskell. let that = Const 2 (* A value is one of the listed cases. *)
"string catenation" = "string " ^ "catenation"
The tags allow us to extract components of a variant value as well as to case against
values by inspecting their tags. This is pattern matching.
Seq.toList "woah" // ⇒ [’w’; ’o’; ’a’; ’h’]
 match· · · with· · · let’s us do case analysis; underscore matches anything.
Printf.printf "%d %s" 1972 "taxi";;
 Patterns may be guarded using when.
let input = System.Console.ReadLine()
 Abbreviation for functions defined by pattern matching: function cs ≈ fun x
-> match x with cs
Records
let rec eval = function
Records: Products with named, rather than positional, components. | Undefined as u -> failwith "Evil" (* Throw exception *)
type Person = {Name: string; Work: string} | Var x -> 0 + match x with "x" -> 999 | _ -> -1
| Const n when n <= 9 -> 9
(* Construction *) | Sum (l, r) -> eval l + eval r
let jasim = {Name = "Jasim"; Work = "Farm"} | _ -> 0 (* Default case *)

(* Pattern matching for deconstruction *) 4 = eval that


let {Name = who; Work = where} = jasim -1 = (Var "nine" |> eval)
// ⇒ who = "Jasim" && where = "Farm" 999 = eval (Var "x")
let {Name = woah} = jasim // ⇒ woah = "Jasim" 0 = eval (Const 10)
let go {Name = qx; Work = qy} = qx.Length + 2
(* Type aliases can also be formed this way *)
(* Or we can use dot notation -- usual projections *) type myints = int
let go’ p = p.Name ^ p.Work let it : myints = 3

(* Or using explicit casing *) Note that we can give a pattern a name; above we mentioned u, but did not use it.
let go’’ x =
 Repeated & non-exhaustive patterns trigger a warning; e.g., remove the default
match x with
case above.
| {Name = n} -> n
| _ -> "Uknown"  You can pattern match on numbers, characters, tuples, options, lists, and arrays.
(* “copy with update” *) ◦ E.g., [| x ; y ; z|] -> y.
let qasim = {jasim with Name = "Qasim"}
Builtins: Options and Choice —these are known as Maybe and Either in Haskell.
Types are “open”, as in Ruby.
type Person with type ’a Option = None | Some of ’a
member self.rank = self.Name.Length type (’a, ’b) Choice = Choice1Of2 of ’a | Choice2Of2 of ’b

qasim.rank // ⇒ 5 See here for a complete reference on pattern matching.

2
Tuples and Lists Options

Tuples: Parentheses are optional, comma is the main operator. Option: Expressing whether a value is present or not.
(* type ’a option = None | Some of ’a *)
let mytuple : int * string * float = (3, "three", 3.0)
let divide x y = if y = 0 then None else Some (x / y)
(* Pattern matching & projection *) None = divide 1 0
let (woah0, woah1, woah2) = mytuple
let add_1and4 (w, x, y, z) = w + z let getInt ox = match ox with None -> 0 | Some x -> x
let that = fst ("that", false) 2 = getInt (Some 2)
(* A singelton list of one tuple !!!! *)
let zs = [ 1, "two", true ] Side Effects —Unit Type
(* A List of pairs *) Operations whose use produces a side-effect return the unit type. This’ akin to the role
[’a’,0 ; ’b’,1 ; ’c’, 2] played by void in C. A function is a sequence of expressions; its return value is the value
of the final expression —all other expressions are of unit type.
(* Lists: type ’a list = [] | (::) of ’a * ’a list *)
let xs = [1; 2; 3] (* type unit = () *) let my_io () = printfn "Hello!"
[1; 2; 3] = 1 :: 2 :: 3 :: [] (* Syntactic sugar *) let ex : unit = ()
let first x y
(* List catenation *) let myupdate (arr : ’a array) (e : ’a) = my_io ()
[1;2;4;6] = [1;2] @ [4;6] (i : int) : unit let _ = y
(* Pattern matching example; Only works on lists of length 3 *) = Array.set arr i e x
let go [x; y; z] = x + y + z
14 = go [2;5;7] let nums = [| 0; 1; 2|] let res = first 1972 12
myupdate nums 33 1
(* Crashes: Incomplete pattern matching *) 33 = nums.[1]
match [1; 2; 3] with
| [] -> 1
| [x; y] -> x Printing & Integrating with C#
// | (x :: ys) -> x
We may use the %A to generically print something.
Here is [0 ; 3 ; 6 ; 9 ; 12] in a number of ways: // ⇒ 1 2.000000 true ni x [1; 4]
printfn "%i %f %b %s %c %A" 1 2.0 true "ni" ’x’ [1; 4]
[0..3..14] (* Ranges, with a step *)
≈ [for i in 0..14 do if i % 3 = 0 then yield i] (* Guarded comprehensions *) Let’s use C#’s integer parsing and printing methods:
≈ [for i in 0..4 -> 3 * i] (* Simple comprehensions *) let x = System.Int32.Parse("3")
≈ List.init 5 (fun i -> 3 * i) System.Console.WriteLine("hello " + string x)
(* First 5 items of an “unfold” starting at 0 *)

Expected: concat, map, filter, sort, max, min, etc. fold starts from the left of the list, Reads
foldBack starts from the right. reduce does not need an inital accumulator.
.  F# Meta-Tutorial
zs |> List.reduce (+) // ⇒ 9  Learn F# in ~60 minutes —https://learnxinyminutes.com/
(* Example of a simple “for loop”. *)  F# for Fun & for Profit! – EBook
[1..10] |> List.iter (printfn "value is %A")
◦ Why use F#? —A series of posts
 Microsoft’s .Net F# Guide
Arrays use [|· · · |] syntax, and are efficient, but otherwise are treated the same as lists;
Pattern matching & standard functions are nearly identical. E.g., [| 1; 2 |] is an array. ◦ F# Language Reference
 Learn F# in One Video —Derek Banas’ “Learn in One Video” Series
Lazy, and infinite, structures are obtained by ‘sequences’.  Real World OCaml —F# shares much syntax with OCaml
 F# Wikibook

You might also like