MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Record TypesBearbeiten
MutabilityBearbeiten
Discriminated UnionsBearbeitentype 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 TypesBearbeitentype 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 FlagsBearbeitenEnums 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
ArraysBearbeiten
// 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..]
Array module[2]Bearbeitenlet arrayOfZeroes = Array.zeroCreate 10
let emptyArray = Array.empty
let appendedArray = Array.append array1 array2
let filteredArray = Array.filter (fun element -> element % 2 = 0) array
ListsBearbeiten
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 Module[3]BearbeitenList.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 MatchingBearbeitenlet 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
SequencesBearbeitenlazily evaluated list seq { 0..10 }
seq { 1..10..100 }
seq { for i in 1..10 -> i * i }
Seq Module[4]BearbeitenSeq.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
SetsBearbeiten
// 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 Module[5]BearbeitenSet.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
MapsBearbeiten
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) ]
Maps Module[6]BearbeitenMap.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
OptionsBearbeitenlet evalOption (o:int option) =
match o with
| Some a -> sprintf "Value is %d" a
| None -> sprintf "None"
let some = Some 1
let none = None
StringsBearbeitenlet str = "this is a string"
let str = @"http://de.wikipedia.org/"
let str = """this is a "quoted" string"""
String Module[7]BearbeitenString.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
QuellenBearbeiten
|