Typbezeichner und Werte beginnen mit Großbuchstaben
Bool hat genau zwei mögliche Werte
True und False sind die Konstruktoren des Typs Bool
Konstruktoren müssen einmalig sein
Verwendung neuer Datentypen
data CardDir = North | South | East | West
Verwendung von CardDir
Datentypen mit Parametern
data Date = Datum Year Month Daytype Year = Intdata Month = Jan | Feb | Mar | ... type Day = Int
Was ist bei Date der Konstruktor? – Datum, eine dreistellige Funktion:
> :t DatumDatum :: Year -> Month -> Day -> Date
Verwendung von Date Selektor
day :: Date -> Dayday (Datum y m d) = d month :: Date -> Monthmonth (Datum y m d) = m
Warum “Algebraische” Datentypen?
Algebraische Datentypen verallgemeinert
Kartesische Produkte UND Disjunktion von Werten und
Rekursive Algebraische Datenstrukturen
data Shape = Circle Float | Rect Float Floatsquare :: Float -> Shapesquare a = Rect a a area :: Shape -> Floatarea (Circle r) = pi * r^2area (Rect x y) = x * y
Haskell Maybe
data Maybe a = Nothing | Just a
vordefinierte Funktionen
import Data.MaybefromJust :: Maybe a -> a > fromJust (Just 42) 42-- es gibt auch maybeToList :: Maybe a -> [a] -- aber:> maybeToList Nothing [][]