Guest User

Untitled

a guest
Jan 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. {-# LANGUAGE ExistentialQuantification #-}
  2.  
  3. import Control.Monad
  4.  
  5. data Cube = Cube { side :: Float }
  6. data Sphere = Sphere { radius :: Float }
  7.  
  8. class Volume a where
  9. volume :: a -> Float
  10.  
  11. instance Volume Cube where
  12. volume = (^ 3) . side
  13.  
  14. instance Volume Sphere where
  15. volume = (4 * pi / 3 *) . (^3) . radius
  16.  
  17. data AnyPolygon = forall v. Volume v => AP v
  18.  
  19. volumeObjects :: [AnyPolygon]
  20. volumeObjects = [AP (Cube 5), AP (Sphere 10)]
  21.  
  22. printVolume :: Float -> IO ()
  23. printVolume = putStrLn . (++) "The volume is: " . show
  24.  
  25. getVolume :: AnyPolygon -> Float
  26. getVolume (AP x) = volume x
  27.  
  28. main = forM_ volumeObjects (printVolume . getVolume)
Add Comment
Please, Sign In to add comment