Listen
F#-Listen sind unveränderlich und verkettet:
let l = [ 5; 3; 9; 1; 7 ]
printfn "%A" l
printfn "%d %d %d" l.Length l.Head (List.last l)
printfn "%A" (0 :: l)
printfn "%A" (l @ [ 8; 9 ])
printfn "%A" (List.rev l)
printfn "%A" (List.sort l)
printfn "%A" (List.sortDescending l)
printfn "%A" (List.map (fun x -> x * 2) l)
printfn "%A" (List.filter (fun x -> x > 4) l)
printfn "%d %d" (List.sum l) (List.max l)
printfn "%d" (List.fold (fun acc x -> acc + x) 0 l)
printfn "%d" (List.reduce max l)
printfn "%A" (List.tryFind (fun x -> x > 5) l)
printfn "%A" (List.tryFind (fun x -> x > 50) l)
printfn "%b %b" (List.exists (fun x -> x > 8) l) (List.forall (fun x -> x > 0) l)
printfn "%A" (List.take 2 l)
printfn "%A" (List.skip 3 l)
printfn "%A" (List.zip [ 1; 2; 3 ] [ "a"; "b"; "c" ])
printfn "%A" (List.partition (fun x -> x % 2 = 0) [ 1 .. 6 ])
printfn "%A" (List.groupBy (fun x -> x % 3) [ 1 .. 7 ])
printfn "%A" (List.collect (fun x -> [ x; x ]) [ 1; 2 ])
printfn "%A" (List.distinct [ 1; 2; 2; 3; 3; 3 ])
printfn "%A" (List.chunkBySize 2 [ 1 .. 5 ])
printfn "%A" (List.windowed 2 [ 1; 2; 3; 4 ])
printfn "%A" (List.init 5 (fun i -> i * i))
printfn "%A" [ for i in 1 .. 5 -> i * i ]
printfn "%A" [ for i in 1 .. 10 do if i % 2 = 0 then yield i ]
printfn "%A" (List.sortBy (fun s -> String.length s) [ "ccc"; "a"; "bb" ])
printfn "%A" (List.pairwise [ 1; 2; 3 ])
printfn "%A" (List.countBy (fun x -> x % 2 = 0) [ 1; 2; 3; 4; 5 ])Ausgabe
[5; 3; 9; 1; 7] 5 5 7 [0; 5; 3; 9; 1; 7] [5; 3; 9; 1; 7; 8; 9] [7; 1; 9; 3; 5] [1; 3; 5; 7; 9] [9; 7; 5; 3; 1] [10; 6; 18; 2; 14] [5; 9; 7] 25 9 25 9 Some 9 None true true [5; 3] [1; 7] [(1, "a"); (2, "b"); (3, "c")] ([2; 4; 6], [1; 3; 5]) [(1, [1; 4; 7]); (2, [2; 5]); (0, [3; 6])] [1; 1; 2; 2] [1; 2; 3] [[1; 2]; [3; 4]; [5]] [[1; 2]; [2; 3]; [3; 4]] [0; 1; 4; 9; 16] [1; 4; 9; 16; 25] [2; 4; 6; 8; 10] ["a"; "bb"; "ccc"] [(1, 2); (2, 3)] [(false, 3); (true, 2)]
Pattern Matching auf Listen
let rec summe liste =
match liste with
| [] -> 0
| kopf :: rest -> kopf + summe rest
let rec laenge = function
| [] -> 0
| _ :: rest -> 1 + laenge rest
let beschreibe = function
| [] -> "leer"
| [ x ] -> sprintf "ein Element: %d" x
| [ x; y ] -> sprintf "zwei Elemente: %d %d" x y
| x :: _ -> sprintf "beginnt mit %d" x
printfn "%d %d" (summe [ 1; 2; 3; 4 ]) (laenge [ "a"; "b" ])
for l in [ []; [ 1 ]; [ 1; 2 ]; [ 7; 8; 9 ] ] do printfn "%s" (beschreibe l)Ausgabe
10 2 leer ein Element: 1 zwei Elemente: 1 2 beginnt mit 7
Arrays, Maps und Sets
let a = [| 5; 3; 9; 1 |]
a.[0] <- 50
printfn "%A %d" a a.Length
printfn "%A" (Array.sort a)
printfn "%A" (Array.map (fun x -> x + 1) a)
printfn "%A" (Array.sum a)
printfn "%A" a.[1..2]
let m = Array2D.init 2 3 (fun i j -> i * 10 + j)
printfn "%A" m
printfn "%d" m.[1, 2]
let alter = Map.ofList [ "Mia", 17; "Tom", 25; "Zoe", 31 ]
printfn "%d" alter.["Tom"]
printfn "%A" (Map.tryFind "Ben" alter)
printfn "%A" (alter |> Map.add "Ben" 12 |> Map.count)
printfn "%A" (alter |> Map.toList)
printfn "%A" (alter |> Map.filter (fun _ v -> v > 20) |> Map.keys |> Seq.toList)
printfn "%b" (alter.ContainsKey "Mia")
let s = Set.ofList [ 3; 1; 2; 3; 1 ]
printfn "%A %d" s s.Count
printfn "%A" (Set.union s (Set.ofList [ 9 ]))
printfn "%A" (Set.intersect s (Set.ofList [ 1; 9 ]))
let zaehler =
"mississippi" |> Seq.countBy id |> Seq.sortBy fst |> Seq.toList
printfn "%A" zaehlerAusgabe
[|50; 3; 9; 1|] 4
[|1; 3; 9; 50|]
[|51; 4; 10; 2|]
63
[|3; 9|]
[[0; 1; 2]
[10; 11; 12]]
12
25
None
4
[("Mia", 17); ("Tom", 25); ("Zoe", 31)]
["Tom"; "Zoe"]
true
set [1; 2; 3] 3
set [1; 2; 3; 9]
set [1]
[('i', 4); ('m', 1); ('p', 2); ('s', 4)]Sequenzen: träge Berechnung
let nat = Seq.initInfinite (fun i -> i + 1)
let quadrate = nat |> Seq.map (fun x -> x * x)
printfn "%A" (quadrate |> Seq.take 5 |> Seq.toList)
printfn "%A" (nat |> Seq.filter (fun x -> x % 7 = 0) |> Seq.take 3 |> Seq.toList)
let fib =
Seq.unfold (fun (a, b) -> Some(a, (b, a + b))) (0, 1)
printfn "%A" (fib |> Seq.take 10 |> Seq.toList)
let primzahlen =
seq {
for n in 2 .. 100 do
if Seq.forall (fun d -> n % d <> 0) (seq { 2 .. int (sqrt (float n)) }) then
yield n
}
printfn "%A" (Seq.toList primzahlen)
printfn "%d" (Seq.sum (seq { 1 .. 100 }))
printfn "%A" (seq { 1 .. 3 } |> Seq.append (seq { 10; 20 }) |> Seq.toList)Ausgabe
[1; 4; 9; 16; 25] [7; 14; 21] [0; 1; 1; 2; 3; 5; 8; 13; 21; 34] [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71; 73; 79; 83; 89; 97] 5050 [10; 20; 1; 2; 3]
Merke
- Listen (
[1; 2; 3]), Arrays ([| ... |]), Maps, Sets und Sequenzen (seq) - Das Modul
List(undArray,Seq) bietetmap,filter,fold,sortBy,groupBy, ... - Pattern Matching zerlegt Listen:
[],kopf :: rest - Sequenzen werden erst bei Bedarf berechnet (unendliche Folgen möglich)
Aufgabe
Zähle mit Seq.countBy die Wörter eines Satzes.