Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE ExistentialQuantification #-}
- import Control.Monad
- data Cube = Cube { side :: Float }
- data Sphere = Sphere { radius :: Float }
- class Volume a where
- volume :: a -> Float
- instance Volume Cube where
- volume = (^ 3) . side
- instance Volume Sphere where
- volume = (4 * pi / 3 *) . (^3) . radius
- data AnyPolygon = forall v. Volume v => AP v
- volumeObjects :: [AnyPolygon]
- volumeObjects = [AP (Cube 5), AP (Sphere 10)]
- printVolume :: Float -> IO ()
- printVolume = putStrLn . (++) "The volume is: " . show
- getVolume :: AnyPolygon -> Float
- getVolume (AP x) = volume x
- main = forM_ volumeObjects (printVolume . getVolume)
Add Comment
Please, Sign In to add comment