Created
August 25, 2016 04:20
-
-
Save lispyclouds/0ccfb8a4147e37f6ded0b417db8fd084 to your computer and use it in GitHub Desktop.
Sattar_Fix
This file contains 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
import Control.Monad.Trans.State | |
data Cycle = Cycle { brand :: String, id :: Int, price :: Int } deriving Show | |
data Store = Store { cycles :: [Cycle], rentals :: [Rental]} deriving Show | |
data Rental = Rental { cycle :: Cycle, rentedOn :: Int, returnedOn :: Maybe Int } deriving Show | |
cycles' :: [Cycle] | |
cycles' = [ Cycle "BSA" 1 10 | |
, Cycle "BSA 2" 2 100 | |
, Cycle "BSA 3" 3 200 ] | |
store :: Store | |
store = Store { cycles = cycles' | |
, rentals = [] | |
} | |
rent :: Store -> Int -> Int -> Store | |
rent store rentedId rentedOn' = Store { cycles = newCycles, rentals = newRentals } | |
where | |
cycle = head . filter (\(Cycle _ id' _) -> id' == rentedId) $ (cycles(store)) | |
newCycles = filter (\(Cycle _ id' _) -> id' /= rentedId) (cycles(store)) | |
newRentals = Rental cycle rentedOn' Nothing : rentals(store) | |
rentS :: Int -> Int -> State Store | |
rentS rentedId rentedOn' = do | |
oldState <- get | |
put (rent oldState rentedId rentedOn') | |
main = do | |
putStrLn $ evalState (rentS 1 1) store |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment