Benutzer:MovGP0/F♯

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Better Datastructures (Types, Discriminated Unitons) by default

  • Immutable
  • Structural equality
  • no null's

Arrays, Lists, Sequences, Tuples

[Bearbeiten | Quelltext bearbeiten]
Array List Sequence Tuple
// define an array
let array = [| 0; 1; 2; 3 |]

let array = 
   [|
      0
      1
      2
      3
    |]

let array = [| for i in 0..3 -> i |]

// access array
array.[5] // 5th element
array.[..2] // 0th to 2nd element
array.[2..] // 2nd to nth element
matrix.[1.., *] // 1st to nth row-array of matrix
// define a List
let list = [0; 1; 2; 3]
let list = [0 .. 100]
let list = [0 .. 0.1 .. 10]

// List concatenate
let list = [0; 1] @ [2; 3]
let newList = -1 :: list

// List methods
list.Lenght // 4
list.Head   // 0
list.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"
(*
Multiline comment
*)
// Single line comment
///<summary>XML documentation comment</summary>
Date,Open,High,Low,Close,Volume,Adj Close
2013-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.36
2013-06-04,53.74,53.75,52.22,52.59,10614700,52.59
2013-06-03,53.86,53.89,52.40,53.41,13127900,53.41
2013-05-31,54.70,54.91,53.99,54.10,12809700,54.10
2013-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 System
open 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

[Bearbeiten | Quelltext bearbeiten]
  • 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!"
  • FParsec. Abgerufen am 17. April 2015 (englisch, Parser Library in F#).