F# Cheat Sheet - PDF by tamir13

VIEWS: 134 PAGES: 7

									                                                F# Cheat Sheet
F# Cheat Sheet                              2. Strings                                 Other conversion functions:
This sheet glances over some of the                                                    float float32 int int16
common syntax of the F# language. It        In F# Code the type string is equivalent
is designed to be kept close for those      to System.String                           4 Tuples
times when you need to jog your             let s = "This is a string"
memory on something like loops or                                                      Construction
                                            let hello = "Hello"+" World"               let x = (1,"Hello")
object expressions. Everything is
designed to be used with the #light         Preserve all characters                    Deconstruction
syntax directive. If you type any of        let share = @"\\share"                     let a,b = x
these lines directly into the interactive
command shell, be sure to follow them       Use escape characters                      Reconstruction and value reuse
up with two semicolons “;;”.                let shareln = "\\\\share\n"                let y = (x,(a,b))
If you have any comments, corrections
or suggested additions please send          3 Numbers                                  Reconstruction into a 3 tuple (triple)
                                                                                       let z = (x,y,a)
them to chance@a6systems.com.
                                            type is int16 = System.Int16               Partial deconstruction triple
1. Comments                                 let int16num = 10s                         let ((a',b'),y',a'') = z

There are a few different kinds of          type is int32 = System.Int32
                                            let int32num = 10
                                                                                       5 Lists, Arrays, Seqs : Generation
comments in F#. Comment blocks,
which are placed between (* and *)          type is int64 = System.Int64               Creates the list [0 ; 2 ; 4]
markers.                                    let int64num = 10L                          let lsinit = List.init 3
                                                                                                    (fun i -> i * 2)
Line by line comments which follow //       type is float32, single or System.Single
until the end of a line and xml doc         let float32num = 10.0f                     Creates same list as above
                                                                                       let lsgen = [ 0 ; 2 ; 4]
comments which follow /// and allow
                                            type is float, double or System.Double
the programmer to place comments in         let floatnum = 10.0                        Creates the list [0;2;4;6;8]
xml tags that can be used to generate                                                  let lsgen2 = [0 .. 2 .. 8]
xml documents.                              convert to int64
                                            let int64frm32 = int64 int32num


                                                                                                                                1

                                            http://a6systems.com
                                           F# Cheat Sheet
Can also do above one increment at a   Same as above one increment at a time    6 Lists, Arrays, Seqs : Consuming
time to get [0;1;2;3;4;5;6;7;8]        to get [|0;1;2;3;4;5;6;7;8|]
let lsgen2' = [0..8]                   let argen2' = [|0..8|]                   "left" fold starts from the left of the list,
                                                                                the "right" fold does the opposite
Creates a list [0.0; 0.5; 1.0; 1.5]    Create an array [0.0; 0.5; 1.0; 1.5]     List.fold_left
let lsgen3 =                            let argen3 =                                  (fun state a -> state + 1 ) 0
   [for i in 0..3 -> 0.5 * float i]       [|for i in 0..3 -> 0.5 * float i|]          [ for i in 0 .. 9 -> true]

Put other steps into a generator       Put other computation steps into the     Reduce doesn’t require the starter
let lsgen3' =                          generator                                argument
      [for i in 0..3 ->                let argen3' =                            List.reduce_left
            printf "Adding %d\n" i          [|for i in 0..3 ->                      (fun accum a -> accum + a )
            0.5 * float i]                         printf "Adding %d\n" i           [0..9]
                                                   0.5 * float i|]
Place -1 at the head of a list                                                  Square all of the elements in a list
let inserted = -1 :: lsgen2'           Creating a seq -- remember these are     List.map (fun x -> x * x) [1..10]
                                       lazy
Concatenation                          let s =                                  Prints all the items of a list
let concat = lsgen2 @ lsgen2'            seq { for i in 0 .. 10 do yield i }    List.iter
                                                                                 (fun x -> printf "%d" x) [1..10]
Create an array [|0 ; 2 ; 4|]          Illustrate laziness – consume the seq
let arinit = Array.init 3              below and note the difference from the   Same examples for arrays
             (fun i -> i * 2)          generated array.                         Array.fold_left
                                       let s2 =                                     (fun state a -> state + 1 ) 0
Create same array as above                 seq { for i in 0 .. 10 do                [| for i in 0 .. 9 -> true|]
let argen = [| 0 ; 2 ; 4|]                         printf "Adding %d\n" i
                                                   yield i }                    Array.reduce_left
                                                                                    (fun accum a -> accum + a )
                                                                                    [|0..9|]

Create the array [|0;2;4;6;8|]
let argen2 = [|0 .. 2 .. 8|]
                                                                                Squares all the elements in the array
                                                                                Array.map
                                                                                   (fun x -> x * x) [| 1 .. 10 |]


                                                                                                                           2

                                       http://a6systems.com
                                              F# Cheat Sheet
                                          8 Composition Operators
Prints all the items of an array          the |> operator is very helpful for       Apply those functions iteratively
Array.iter                                chaining arguments and functions          listOfPrintActions
   (fun x -> printf "%d" x)               together                                  |> List.iteri (fun i a -> a i)
   [|1..10|]                              let piped = [0..2] |> List.sum
                                                                                    Anonymous function (applied to 2)
Access all elements of an array from 2    the >> operator is very helpful for       (fun x -> x * x) 2
on                                        composing functions
let arr = [|for i in 0..3 -> i|]          open System                               Anonymous function (applied to
arr.[2..]                                 let composedWriter =                      tuple,which is deconstructed inside)
                                               string >>                            let arg = (3,2)
Access elements between 2 and 4                Console.WriteLine                    (fun (x,y) -> x * y) arg
(inclusive)
let arr = [|for i in 0..3 -> i|]          9 Functions as values                     10 Union Types
arr.[2..4]
                                          Create a function of 3 arguments          Discriminated Union
Access all elements of an array up to 4   let add x y z = x + y + z                  type option<'a> =
let arr = [|for i in 0..3 -> i|]                                                        | Some of 'a
arr.[..4]                                                                               | None
                                          Currying example
                                          let addWithFour= add 4
Seq also has iter, fold, map and reduce
Seq.reduce
    (fun accum a -> accum + a)
                                                                                    Augmented Discriminated Union
                                          Apply remaining arguments                 type BinTree<‟a> =
    (seq { for i in 0 .. 9 do
                                          addWithFour 2 10                             | Node of
           yield i })
                                                                                          BinTree<‟a> * „a *
                                                                                          BinTree<‟a>
                                          Take a function as an argument               | Leaf
 7 Arrays: Manipulating                   let runFuncTenTimes f a =                   with member self.Depth() =
                                              [ for 0..9 -> f a]                           match self with
Array elements can be updated                                                              | Leaf -> 0
                                          Return a list of functions as arguments          | Node(l,_,r) -> 1 +
let arrayone = [|0..8|]
                                          let listOfPrintActions =                                     l.Depth() +
   arrayone.[0] <- 9
                                                [ for 0 .. 10 ->                                       r.Depth()
                                                        printf “%s\n”]

                                                                                                                           3

                                          http://a6systems.com
                                         F# Cheat Sheet
11 Types: Records                     Subclass                             Object Expressions
                                      type MyClass() =                     let foo =
type Person = {name:string;age:int}     inherit BaseClass()                  {new MyAbsFoo with
                                        let someval = “SomeVal”                  member o.Foo()=”Bar”}
let paul = {name="Paul";age=35}         let mutable myIntValue = 1
                                        member self.SomeMethod(x,y) =      Augmenting Existing Objects (note:
let paulstwin =                                  gxy
                                                                           augmented members only available
   {paul with name="jim"}               static member StaticMethod(x,y)=
                                                 fxy                       when augmenting module is opened)
                                        member override o.InheritNum() =   open System.Xml
do printf "Name %s, Age %d"                      base.InheritNum()+        type XmlDocument() =
       paul.name paul.age                        myIntValue                    member o.GetInnerXml() =
                                                                                     self.InnerXml
Augmenting Records
                                                                           Static Upcasting
                                      Interface                            let strAsObj =
type Person = {name:string;age:int}   type MyAbsFoo =
      with member o.Tupilize() =                                             let str = “Hello”
                                          abstract Foo:unit->string
          (o.name,o.age)                                                     str :> obj
                                      type MyFooClass() =
                                          let mutable myfoo =”Foo”         Dynamic Downcasting
12 Types: OOP                             member o.MyFoo                   let objSub (o:‟a when „a:>object) =
                                                 with get () = myfoo         o :?> SomeSubType
Classes                                          and set v = myfoo<-v
                                          interface MyAbsFoo with          13 Pattern Matching
                                            member o.Foo() = myfoo
type BaseClass()=                         end
   let mutable myIntValue=1                                                Basic
   member o.Number                                                         let f (x:option<int>) =
       with get() = myIntValue                                               match x with
       and set v = myIntValue<-v                                             | None -> ()
   abstract member                                                           | Some(i) -> printf “%d” i
          InheritNum:unit->int
   default o.InheritNum() =                                                As a function definition
             o.Number + 1                                                  let f = function
                                                                             | None -> ()
                                                                             | Some(i) -> printf “%d” i

                                                                                                                 4

                                      http://a6systems.com
                                           F# Cheat Sheet
                                       Add block that runs whether exception   15 Loops
With when operation                    is thrown or not                        for i in 0..10 do
let f = function                       try                                       printf “%d” i
  | None -> ()                           obj.SomeOp()                          done
  | Some(i) when i=0 -> ()             finally
  | Some(i) when i>0 ->printf“%d”i       obj.Close()                           Over an IEnumerable
                                                                               for x in xs do
Common matches on a literal            Raise an exception in code                printf “%s\n”(x.ToString())
let f x =                                                                      done
                                       -Shorthand
  match x with                         let f x =
  | 0 | 1 as y -> f y                     if not x.Valid then
                                                                               While
  | i -> printf “%d” i                      invalid_arg “f:x is not valid”     let mutable mutVal = 0
                                          else x.Process()                     while mutVal<10 do
                                                                                  mutVal <- mutVal + 1
Wildcard                                                                       done
let f = function                       -Full
  | 0 | 1 as y -> printf “Nothing”     let f x =
  | _ -> printf “Something”               if not x.SupportsProcess() then
                                                                               16 Async Computations
                                            raise                              (Note: FSharp.PowerPack.dll should
14 Exceptions                                (InvalidOperationException        be referenced in your project – as of the
try                                            (“x must support process”))     CTP - to get the augmented async
  obj.SomeOp()                            else x.Process()                     methods available in existing IO
with | ex ->                                                                   operations)
       printf “%s\n” ex.Message
                                       Create your own                         Basic computation that returns
With (exception) type test             exception InvalidProcess of string
try
                                                                               Async<int> that will yield 1 when
  obj.SomeOp()                         try                                     executed
with                                    raise InvalidProcess(“Raising Exn”)    let basic = async { return 1 }
  | :? ArgumentException as ex ->      with
       printf “Bad Argument:\n”         | InvalidProcess(str) ->               Composing expressions and applying to
  | exn -> printf “%s\n” exn.Message        printf “%s\n” str                  arguments
                                                                               let compound num =
                                                                                      async {
                                                                                              let! anum = basic
                                                                                              return num + anum }

                                                                                                                      5

                                       http://a6systems.com
                                                      F# Cheat Sheet
                                                   Making sure I/O threads don’t block
Returning existing expressions                     (Note the MethodAsync convention in           Partial Pattern
let composedReturn =                               “Expert F#” seems to have changed to          let (|Xml|_|) doc =
       async { return! compound 2}                 AsyncMethod)                                        if doc.InnerXml=”” then None
                                                                                                       else Some(doc.InnerXml)
                                                   let asyncRead file (numBytes:int)=
Creating Primitives with existing                    async {                                     let getXml = function
                                                          let inStr = File.OpenRead(file)              | Xml(xml) -> Some(xml) //Xml Matched
Begin/End Async Calls                                                                                  | _ -> None          // Xml did not match
                                                          let! data = inStr.AsyncRead numBytes
let asyncCall args =
                                                          return processData(data) }
   Async.BuildPrimitive
      ((fun (callback,asyncState) ->
         myService.BeginMethod(args,               Execution Methods (apply the async            18 Compiler Directives and Interop
                                   callback,       computation as an argument to these)          with other .NET Languages
                                   asyncState)),   Async.Run                                     Make indentation significant in parsing
         myService.EndMethod)                      Async.Spawn                                   (i.e. turn on light syntax)
                                                   Async.SpawnFuture                             #light
Make your own primitive from scratch               Async.SpawnThenPostBack
let asyncPrimitive args =                                                                        Reference a DLL from another .NET
       Async.Primitive (fun (con,exn) ->           17 Active Patterns
           let result = runSomething args
                                                                                                 library (interactive F# scripts only – in
                                                   Basic                                         compiled code use normal interface for
           if good result then con result          let (|Xml|) doc = doc.InnerXml
          else exn result)                                                                       reference additions)
                                                                                                 #r @“.\src\bin\mylib.dll”
                                                   let getXml = function
Other primitives                                     | Xml(xml) -> xml
Async.Parallel                                                                                   Include a directory in the reference
Async.Primitive                                    Multiple Patterns                             search (also in interactive scripts only)
Async.Catch                                        let (|Xml|NoXml|) doc =                       #I @“[dir path]”
                                                         if doc.InnerXml=”” then NoXml
                                                         else Xml(doc.InnerXml)                  For a C# class Foo in a dll with a
                                                                                                 method ToString(), invoke just as you
                                                   let getXml = function
                                                                                                 would an F# class.
                                                         | Xml(xml) -> Some(xml)
                                                                                                 let foo = Foo()
                                                         | NoXml -> None
                                                                                                 let s = foo.ToString()


                                                                                                                                                   6

                                                   http://a6systems.com
                                              F# Cheat Sheet
To have code run only in when
working with the compiled version
#if COMPILED
…code
#endif

For example, when writing a windowed
application that you test in script, but
eventually compile to run
let window =
   Window(Title=”My Window”)
#if COMPILED
[<STAThread>]
do
  let app = Application in
  app.Run(window) |> ignore
#endif
… later in script (.fsx) file …
window.Show()


Version 1.01
You can always get the most recent
updates to this cheat sheet from
http://a6systems.com/fsharpcheatsheet.pdf

A6 Systems, LLC is an Austin, TX
based company that provides
consulting services and F# QuickStart
training.



                                                                   7

                                            http://a6systems.com

								
To top