Was ist F#?
F# (ausgesprochen F Sharp, 2005, Microsoft Research / Don Syme) ist eine funktionale Sprache für die .NET-Plattform mit starker Typinferenz, Unveränderlichkeit als Standard und Pattern Matching. F# ist objektorientiert und imperativ erweiterbar und nutzt alle .NET-Bibliotheken. Die Syntax wird durch Einrückung strukturiert, wie in Python.
Typische Einsatzgebiete: Finanz- und Versicherungsmathematik, Datenanalyse (mit Notebooks), Compiler und Parser, domänengetriebener Entwurf und Web-Backends (Giraffe, Saturn). Mit Fable übersetzt man F# nach JavaScript.
Ausführen
dotnet new console -lang "F#" -o hallo # Projekt anlegen
cd hallo && dotnet run # bauen und starten
dotnet fsi # interaktive Konsole (F# Interactive)
dotnet fsi skript.fsx # Skript ausführen
dotnet add package FSharp.Data # NuGet-PaketDas erste Programm
printfn "Hallo, Welt!"
printfn "1 + 2 = %d" (1 + 2)
printf "ohne Zeilenumbruch"
printfn ""
let name = "Mia"
printfn "Hallo, %s!" name
printfn $"Interpoliert: {name} hat {name.Length} Buchstaben"
printfn "%s ist %d Jahre alt und %.1f m groß" name 17 1.68Hallo, Welt! 1 + 2 = 3 ohne Zeilenumbruch Hallo, Mia! Interpoliert: Mia hat 3 Buchstaben Mia ist 17 Jahre alt und 1.7 m groß
Wird eine .fs-Datei als letzte Datei eines Projekts übersetzt, laufen ihre obersten Anweisungen als Hauptprogramm. printfn ist typsicher: Format %d verlangt int, %s einen String, %f eine Fließkommazahl.
let: unveränderliche Werte
let alter = 17
let preis = 9.99
let name = "Mia"
let ok = true
let z = 'z'
let gross = 9_000_000_000L
let dezimal = 1.5m
let einheit = ()
printfn "%d %.2f %s %b %c" alter preis name ok z
printfn "%d %M" gross dezimal
printfn "%A %A %A" alter preis name
printfn "%s %s" (alter.GetType().Name) (preis.GetType().Name)
let mutable zaehler = 0
zaehler <- zaehler + 5
printfn "%d" zaehler
let schatten () =
let x = 10
let x = x * 2 // Shadowing: neuer Wert mit gleichem Namen
x
printfn "%d" (schatten ())17 9.99 Mia true z 9000000000 1.5 17 9.99 "Mia" Int32 Double 5 20
let bindet unveränderlich. Veränderliche Werte braucht man selten und kennzeichnet sie mit mutable und <-.
Rechnen
printfn "%d %d %d %d" (7 + 2) (7 - 2) (7 * 2) (7 / 2)
printfn "%d %d" (7 % 3) (-7 % 3)
printfn "%.3f %.3f" (7.0 / 2.0) (2.0 ** 10.0)
printfn "%d" (pown 2 10)
printfn "%.4f %d %d" (sqrt 2.0) (abs -5) (max 3 9)
printfn "%d %d %d" (int 3.99) (int (round 2.5)) (int (floor -2.1))
printfn "%f" (float 7 / 2.0)
printfn "%b %b %b" (3 < 5) (3 = 3) (3 <> 3)
printfn "%b %b" (true && false) (true || false)
printfn "%d %d %d" (12 &&& 10) (12 ||| 10) (12 ^^^ 10)
printfn "%d %d" (1 <<< 4) (256 >>> 4)
printfn "%s" (string 42 + "!")
printfn "%d" (int "17" + 1)
printfn "%d" (System.Int32.MaxValue)
printfn "%A" (bigint 2 ** 100)
printfn "%.2f" System.Math.PI9 5 14 3 1 -1 3.500 1024.000 1024 1.4142 5 9 3 2 -3 3.500000 true true false false true 8 14 6 16 16 42! 18 2147483647 1267650600228229401496703205376 3.14
So führst du den Code aus: Datei Program.fs in einem Projekt (dotnet new console -lang F#) speichern und mit dotnet run ausführen; Skripte mit dotnet fsi skript.fsx (.NET SDK von dotnet.microsoft.com).
Merke
- F# ist funktional und typsicher, mit Typinferenz und Einrückungs-Syntax
letbindet unveränderlich,mutableund<-erlauben Änderungprintfn "%d %s"ist typsicher;$"..."interpoliert- Zahlentypen werden nie automatisch umgewandelt
Aufgabe
Berechne die Fläche eines Kreises mit Radius 2,5 und gib sie mit zwei Nachkommastellen aus.