Skip to content

Instantly share code, notes, and snippets.

@blerou
Created November 5, 2012 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blerou/4020961 to your computer and use it in GitHub Desktop.
Save blerou/4020961 to your computer and use it in GitHub Desktop.
Let's play Haskell! - store
-- 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