daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Jan 17th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top