Created
November 5, 2012 22:56
-
-
Save blerou/4020961 to your computer and use it in GitHub Desktop.
Let's play Haskell! - store
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
-- array($year, $model, $price) | |
-- (Int, String, Int) | |
type Price = Int | |
data CarType = Ford | Chevrolet deriving (Show) | |
data Vehicle = Car Int CarType Price | |
| Bicycle Price deriving (Show) | |
type Store = [Vehicle] | |
storePrice :: Store -> Price | |
storePrice [] = 0 | |
storePrice store = price + storePrice remaining | |
where (car:remaining) = store | |
(Car year model price) = car | |
storePrice2 :: Store -> Price | |
storePrice2 [] = 0 | |
storePrice2 (v:remaining) = (price v) + storePrice2 remaining | |
price (Car _ _ p) = p | |
price (Bicycle p) = p | |
sampleStore :: Store | |
sampleStore = [mustang65, corvette63, mustang66, csepel] | |
mustang65 :: Vehicle | |
mustang65 = Car 1965 Ford 1111 | |
mustang66 :: Vehicle | |
mustang66 = Car 1966 Ford 1234 | |
corvette63 :: Vehicle | |
corvette63 = Car 1963 Chevrolet 2222 | |
csepel :: Vehicle | |
csepel = Bicycle 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment