Theory and Design of PL (CS 538)
February 05, 2020
newtype
declarationchar
for character[a]
for list of elements of same typeMkPair
a
and b
give a type Pair a b
data Person = MkPerson
String -- Name
Bool -- Is employed?
Bool -- Is married?
Int -- Age
String -- Address
data Person = MkPerson
{ name :: String -- Name
, employed :: Bool -- Is employed?
, married :: Bool -- Is married?
, age :: Int -- Age
, address :: String -- Address
}
-- Keep all fields the same, except for name and address:
defaultPerson' = defaultPerson
{ name = "Jane Doe"
, address = "456 Main Street, Anytown, WI }
Color
HoursMinutes
and Minutes
Time
in exactly two ways:
HoursMinutes 11 59 :: Time
Minutes 1800 :: Time
Maybe a
is either nothing, or an a
Nothing
case isn’t handledNothing
is usually indicates failureEither
is just a sum with two type parameters:data Either a b = Left a | Right b
-- Auto-generated: Left :: a -> Either a b
-- Auto-generated: Right :: b -> Either a b
Left
or Right
to create an Either a b
Maybe
, do a case analysis:a
: type of list elements