Skip to content

Instantly share code, notes, and snippets.

@lispyclouds
Created August 25, 2016 04:20
Show Gist options
  • Save lispyclouds/0ccfb8a4147e37f6ded0b417db8fd084 to your computer and use it in GitHub Desktop.
Save lispyclouds/0ccfb8a4147e37f6ded0b417db8fd084 to your computer and use it in GitHub Desktop.
Sattar_Fix
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