Benutzer:MovGP0/F♯/Data Structures

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


// delcare Open-High-Low-Close type
type OHLC = {
   openValue: float
   highValue: float
   lowValue: float
   closeValue: float
   }

// create instance
let ohlcBar : OHLC = {
   openValue = 1.3
   highValue: 4.0
   lowValue: 1.0
   closeValue: 1.5
   }
// declare Quote type
type Quote = {
   bid: float
   ask: float
   }
   member this.average() = (this.bid + this.ask) / 2.0

// create instance
let quote : Quote = { 
   bid = 100.0
   ask = 200.0
   }

// call member function
quote.average()

// pattern matching
match matchQuote (q:Quote) with
   | { bid = 0.0; ask = 0.0} -> printfn "invalid quote"
   | { bid = b; ask = a } -> printfn "bid: %f, ask: %f" b a

matchQuote quote
always mutable only mutable in other CLI languages
type Point = {
   mutable x : float
   mutable y : float
   }

let point : Point = { x = 5.0; y = 6.3 }
point.x <- 5
[<CLIMutable>]
type Point = {
   x : float
   y : float
   }

let point : Point = { x = 5.0; y = 6.3 }
point.x <- 5 // compile error

Discriminated Unions

[Bearbeiten | Quelltext bearbeiten]
type OrderSide = 
   | Buy
   | Sell

let buy = Buy
let sell = Sell

let MatchOrderSide (side:OrderSide) = 
   match side
   | Buy -> printf "bying to customer"
   | Sell -> printf "selling to customer"

Alternative Example:

type Side = Bid | Ask

type Order = {
   LimitPrice : decimal
   Quantity : int
   Side : Side
   }

use from C#:

var order = new Order(100m, 10, Side.Bid);

Algebraic Types

[Bearbeiten | Quelltext bearbeiten]
type Tree = 
   | Leaf of int
   | Node of Tree * Tree

let simpleTree = Node (Leaf 1, Leaf 2)

let countLeaves tree = 
   let rec loop sum = function
      | Leaf(_) -> sum + 1
      | Node(leftTree, rightTree) -> sum + (loop 0 leftTree) + (loop 0 rightTree)
   loop 0 tree

Enumerations and Flags

[Bearbeiten | Quelltext bearbeiten]

Enums and Flags in F# [1]

 
// Union
type SizeUnion = 
   | Small 
   | Medium 
   | Large

// Enum
// (strings are not allowed)
type ColorEnum = 
   | Red = 0 
   | Yellow = 1 
   | Blue = 2

// Flags
[<Flags>]
type PermissionFlags = 
   | Read = 1 
   | Write = 2 
   | Execute = 4
  • fixed size
  • fast random access
  • mutable
// declare array
let array = [| 1; 2; 3 |]

// arrays are always mutable
array.[0] <- 10

// compiler error: must be of same type
let array = [| 1.0; 2; 3 |]

let array = [| for i in 1..10 -> i * i |]

// index array with dot index
let top = array.[0]
let topThree = array.[..2]
let tail = array.[1..]
let arrayOfZeroes = Array.zeroCreate 10
let emptyArray = Array.empty
let appendedArray =  Array.append array1 array2
let filteredArray = Array.filter (fun element -> element % 2 = 0) array
  • implemented as linked list
  • insert and head are fast
  • random access is slow
let list = [1..10]
let list = [ for i in 1..10 -> i*i ]

// concat
let list = 0 :: [1; 2; 3]

// append
let list = [0; 1] @ [2; 3]
List.nth a 
List.average a
List.max a
List.min a
List.append a b
List.filter f a
List.empty
List.length a
List.find f a
List.sort a
List.sortBy f a
List.zip a b
List.exists f a
List.fold f s a
List.head a
List.tail a
List.map f a

Pattern Matching

[Bearbeiten | Quelltext bearbeiten]
let rec getLengthOfList length = function 
   | [] -> length
   | head :: tail -> getLengthOfList (length + 1) tail

let myList = [1..10]
getLengthOfList 0 myList

let rec getSecondLastElement = function
   | head :: tail :: [] -> head
   | head :: tail -> getSecondLastElement tail

lazily evaluated list

seq { 0..10 }
seq { 1..10..100 }
seq { for i in 1..10 -> i * i }
Seq.nth i a
Seq.average a
Seq.min a
Seq.max a
Seq.append a b
Seq.filter f a
Seq.empty
Seq.find f a
Seq.sort a
Seq.zip a b
Seq.length a
Seq.exists f a
Seq.fold f s a
Seq.head
Seq.map f a
  • implemented as binary tree
    • no duplicates
    • no preservation of order
  • immutable
// create set from sequence
let seq = [1; 2; 3] 
let set1 = set s
let set2 = Set.ofSeq s

// creates set from array
let arr = [|1; 2; 3|]
Set.ofArray arr
Set.count a
Set.contains e s
Set.empty
Set.fold f s a
Set.exists f a
Set.filter f a
Set.partition f a
Set.map f a
  • implemented as hashtable
let m = Map.empty.Add ("Age", 27)
m.["Age"] // 27

// create map from list of tuples
let m = Map.ofList [ ("Year", 2009); ("Month", 12); ("Day", 3) ]
Map.add k v
Map.empty
Map.filter f m
Map.exists f m
Map.partition f m // returns a tuple of two maps; fst contains matches; snd contains non-matches
Map.containsKey k m
Map.fold f s a
Map.find f a
let evalOption (o:int option) = 
   match o with
   | Some a -> sprintf "Value is %d" a
   | None -> sprintf "None"

let some = Some 1
let none = None
let str = "this is a string"
let str = @"http://de.wikipedia.org/"
let str = """this is a "quoted" string"""
String.length s
String.Empty
String.map f s
String.IsNullOrEmpty s
String.IsNullOrWhiteSpace s
String.Copy s
String.Concat s1 s2
String.exists f s
String.Compare s1 s2
  1. Enum types. In: F# for fun and profit. 9. Juli 2012, abgerufen am 27. Juni 2014 (englisch).
  2. Collections.Array Module (F#). In: MSDN. Microsoft, abgerufen am 27. Juni 2014 (englisch).
  3. Collections.List Module (F#). In: MSDN. Microsoft, abgerufen am 27. Juni 2014 (englisch).
  4. Collections.Set Module (F#). In: MSDN. Microsoft, abgerufen am 27. Juni 2014 (englisch).
  5. Collections.Set Module (F#). In: MSDN. Microsoft, abgerufen am 27. Juni 2014 (englisch).
  6. Collections.Map Module (F#). In: MSDN. Microsoft, abgerufen am 27. Juni 2014 (englisch).
  7. Core.String Module (F#). In: MSDN. Microsoft, abgerufen am 29. Juni 2014 (englisch).