Created
January 9, 2014 20:18
-
-
Save meikj/8341204 to your computer and use it in GitHub Desktop.
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
type Name = String | |
type Customer = (Name, Int, Int, Int) | |
type VStore = [Customer] | |
-- BEGIN TEST DATA -- | |
cust1 :: Customer | |
cust1 = ("Neil", 311, 271, 345) | |
cust2 :: Customer | |
cust2 = ("Fred", 0, 271, 0) | |
vstore :: VStore | |
vstore = [cust1, cust2] | |
-- END TEST DATA -- | |
myloans :: VStore -> Name -> [Int] | |
myloans vs n = loans $ head $ filter (\(name,_,_,_) -> name == n) vs | |
where loans (_,a,b,c) = [l | l <- [a,b,c], l /= 0] | |
members :: VStore -> [Name] | |
members vs = [n | (n,_,_,_) <- vs] | |
owners :: VStore -> Int -> [Name] | |
owners vs id = [n | (n,a,b,c) <- vs, a == id || b == id || c == id] | |
borrow :: VStore -> Name -> Int -> VStore | |
borrow vs n id = map borrow' vs | |
where | |
borrow' (name,a,b,c) | |
| name == n = loan (name,a,b,c) | |
| otherwise = (name,a,b,c) | |
loan (name,a,b,c) | |
| a == 0 = (name,id,b,c) | |
| b == 0 = (name,a,id,c) | |
| c == 0 = (name,a,b,id) | |
| otherwise = (name,a,b,c) | |
return :: VStore -> Name -> Int -> VStore | |
return vs n id = map return' vs | |
where | |
return' (name,a,b,c) | |
| name == n = loan (name,a,b,c) | |
| otherwise = (name,a,b,c) | |
loan (name,a,b,c) | |
| a == id = (name,0,b,c) | |
| b == id = (name,a,0,c) | |
| c == id = (name,a,b,0) | |
| otherwise = (name,a,b,c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment