MovGP0    Über mich    Hilfen    Artikel    Weblinks    Literatur    Zitate    Notizen    Programmierung    MSCert    Physik   


F♯

Advantages

Better Datastructures (Types, Discriminated Unitons) by default

  • Immutable
  • Structural equality
  • no null's

Arrays, Lists, Sequences, Tuples

ArrayListSequenceTuple
// define an arraylet array = [| 0; 1; 2; 3 |]let array =    [|      0      1      2      3    |]let array = [| for i in 0..3 -> i |]// access arrayarray.[5] // 5th elementarray.[..2] // 0th to 2nd elementarray.[2..] // 2nd to nth elementmatrix.[1.., *] // 1st to nth row-array of matrix
// define a Listlet list = [0; 1; 2; 3]let list = [0 .. 100]let list = [0 .. 0.1 .. 10]// List concatenatelet list = [0; 1] @ [2; 3]let newList = -1 :: list// List methodslist.Lenght // 4list.Head   // 0list.Tail   // [1; 2; 3]
let sequence = seq {       yield 0      yield 1      yield! [2..3]      yield! seq {          for i in i..10 do             if i % 2 = 0 then yield i      }   }
let tuple = (0, 1, 2.0, "3") // val it : int * int * float * string
let arrayMatcher array =    match array with   | [||] -> "empty array"   | [|first|] -> sprintf "array has only the one element %A." first   | [|first; second|] -> sprintf "array has the two elements %A and %A." first second   | _ -> "the array has more than two elements"
let listMatcher list =    match list with   | [] -> "empty list"   | [first] -> sprintf "list has only the one element %A." first   | [first; second] -> sprintf "list has the two elements %A and %A." first second   | _ -> "the list has more than two elements"

Comments

(*Multiline comment*)
// Single line comment
///<summary>XML documentation comment</summary>

CSV Example

Date,Open,High,Low,Close,Volume,Adj Close2013-06-06,51.15,51.66,50.83,51.52,9848400,51.52 2013-06-05,52.57,52.68,50.91,51.36,14462900,51.362013-06-04,53.74,53.75,52.22,52.59,10614700,52.592013-06-03,53.86,53.89,52.40,53.41,13127900,53.412013-05-31,54.70,54.91,53.99,54.10,12809700,54.102013-05-30,55.01,55.69,54.96,55.10,8751200,55.10 2013-05-29,55.15,55.40,54.53,55.05,8693700,55.05
open Systemopen System.IO///<summary>Opens the file and returns the content.</summary>let openFile (fileName:string) =    try      let content = File.ReadAllLines fileName      content |> Array.toList   with      | :? FileNotFoundException as e -> (printfn "Exception %s" e.Message                                          [String.Empty])///<summary>Get the row with the lowest trading volume</summary>let lowestVolume filePath =    openFile filePath   |> List.map (fun (l:string) -> l.Split(',')) // split lines to arrays   |> Seq.skip 1 // skip header line of the .csv file   |> Seq.minBy (fun x -> (int x.[5])) // get the row with the lowest trading volume

Namespaces and Modules

  • Files are implicit Modules!
  • Modules are processed in sequential order; File order is important!
  • Namespaces need to be the first declaration in the file
namespace MyCompany.MyNamespace   [<AutoOpen>] // AutoOpenAttribute automatically imports module when namespace is referenced    module MainModule =       // declare public members      let x = 2      let y = 3         // declare nested module      module NestedModule =          let f =             x + y         // declaring private/internal members is explicit      let private greeting = "Hello World!"      let internal greeting = "Hello World!"

Packages

  • FParsec. Abgerufen am 17. April 2015 (englisch, Parser Library in F#).