Created
March 2, 2016 05:01
-
-
Save davidchambers/04129337a84d18a18de4 to your computer and use it in GitHub Desktop.
Gumballs!
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
data Action = Coin | Turn deriving Show | |
data Locked = Locked | Unlocked deriving Show | |
data State = State { locked :: Locked, coins :: Int, gumballs :: Int } deriving Show | |
update :: Action -> State -> State | |
update Coin s@(State { locked = Locked, gumballs = g }) | g == 0 = s | |
update Coin s@(State { locked = Locked, coins = c }) = s { locked = Unlocked, coins = c + 1 } | |
update Turn s@(State { locked = Unlocked, gumballs = g }) = s { locked = Locked, gumballs = g - 1 } | |
update _ s = s | |
main = putStrLn $ show $ update Turn $ update Coin State { locked = Locked, coins = 0, gumballs = 100 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment