Skip to content

Instantly share code, notes, and snippets.

@moodmosaic
Created February 15, 2018 12:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save moodmosaic/a24f10e295773b1597db0af3f1a33f0d to your computer and use it in GitHub Desktop.
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