Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module SumProduct (
KnownColor (..)
, RGB (..)
, redColor
, magenta
, red
) where
-- | To calculate how many possible values the new type has, we count and sum
-- all the possible values, therefore "sum type"
data KnownColor -- the new type's name
= Red -- One possible value
| Blue
| Green
redColor :: KnownColor
redColor =
Red
-- | To calculate how many possible values the new type has, we count and
-- multiply the amount of possible values for each type. Therefore "product
-- type"
--
-- data RGB
-- = MkRGB Int Int Int
{-
^ ^ ^ ^
| | | |
| | | +- This is the blue component
| | |
| | +----- This is the green component
| |
| +--------- This is the red component
|
+------------- This is called the value constructor, or "tag"
-}
magenta :: RGB
magenta =
MkRGB 255 0 255
-- | Records allow us to name the fields in a product type
-- There is more to records, but we won't talk too much about it here
--
data RGB =
MkRGB {
rgbRed :: Int
, rgbGreen :: Int
, rgbBlue :: Int
}
red :: RGB
red =
MkRGB {
rgbRed = 255
, rgbGreen = 0
, rgbBlue = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.