Last active
August 29, 2015 14:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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