Skip to content

Instantly share code, notes, and snippets.

@Decoherence
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Decoherence/145570553270d449b7a9 to your computer and use it in GitHub Desktop.
Save Decoherence/145570553270d449b7a9 to your computer and use it in GitHub Desktop.
Fun with View Patterns: Calculate bonus points for a customer based on their reward status
{-# LANGUAGE ViewPatterns #-}
module Main where
data Status = Silver
| Gold
| Platinum
deriving (Show)
type Points = Int
data Customer = Customer { name :: String
, points :: Points
, status :: Status
} deriving (Show)
bonusPoints :: Customer -> Points
bonusPoints c@(status -> Silver) = points c * 10
bonusPoints c@(status -> Gold) = points c * 100
bonusPoints c@(status -> Platinum) = points c * 1000
bonusPoints _ = 0
main :: IO ()
main = do
putStrLn "\n\tFun with View Patterns\n"
let bob = Customer "Bob" 10 Silver
joe = Customer "Joe" 10 Gold
ann = Customer "Ann" 10 Platinum
print $ bonusPoints bob
print $ bonusPoints joe
print $ bonusPoints ann
{-
Fun with View Patterns
100
1000
10000
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment