Skip to content

Instantly share code, notes, and snippets.

@blerou
Created November 11, 2012 12:16
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/4054740 to your computer and use it in GitHub Desktop.
Save blerou/4054740 to your computer and use it in GitHub Desktop.
Let's play Haskell! - store, the second day
-- array($year, $model, $price)
-- (Int, String, Int)
type Price = Int
data CarType = Ford | Chevrolet deriving (Show)
data Vehicle = Car Int CarType Price
| Bicycle Price
| Motor Price deriving (Show)
data Accessory = SteeringWheel Price | Brake Price deriving (Show)
type Store = [Vehicle]
class Priceable a where
price :: a -> Price
instance Priceable Vehicle where
price (Car _ _ p) = p
price (Bicycle p) = p
instance Priceable Accessory where
price (SteeringWheel p) = p
price (Brake p) = p
storePrice :: (Priceable a) => [a] -> Price
storePrice ps = sum (map price ps)
sampleStore :: Store
sampleStore = [mustang65, corvette63, mustang66, csepel]
sampleAccessories :: [Accessory]
sampleAccessories = [SteeringWheel 12, Brake 23]
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