My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 02, 2007 by berlin.brown
Labels: Featured
UsingStrictAnnotationsDataTypes  
Example with usage of Strict Annotations and Data Types.

Source

{-  Module      : Main
    Copyright   : BSD License
    License     : 
    Maintainer  : Berlin Brown
    Stability   : 
    Portability : 
    Description : Strict Annotation Example
    
    Define a Camera type with pos vectors and then
    print the data.
    
    Also see the following article on
    strictness annotations to data and functions
    http://users.aber.ac.uk/afc/stricthaskell.html#seq
    
    Eg, 
    data Shape = Circle !Int !Int !Colour | Square !Int !Colour    
-}

import Data.IORef

data Camera = Camera {
      cameraPos         :: !(Float, Float, Float),
      cameraAngle       :: !(Float, Float, Float),
      cameraRot         :: !(Float, Float, Float),
      cameraCenterAxis  :: !(Float, Float, Float),
      cameraYaw         :: !(Float, Float, Float),
      cameraPitch       :: !(Float, Float, Float), 
      cameraRoll        :: !(Float, Float, Float)
    } deriving (Read)

{- These 'Show' instances can be converted to character strings 
 for pretty printing the information the data type contains.
 Also see the following for more information types
 (eg, to reference their field names, etc)
 http://www.haskell.org/tutorial/moretypes.html
-}
instance Show Camera where
    show defCamera = "<Camera> (x, y, z) =[" ++ 
    	show (cameraPos defCamera)

-- Initialize a camera
-- Only set the camera position, camera angle and rotation
-- (Position, Camera Angle, Camera Rotation)
initCamera :: (Real posv) => (posv, posv, posv)-> (posv, posv, posv) -> (posv, posv, posv) -> Camera
initCamera (x1, y1, z1) (x2, y2, z2) (x3, y3, z3) = 
    Camera { 
  cameraPos   = ((realToFrac x1), (realToFrac y1), (realToFrac z1)),
  cameraAngle = ((realToFrac x2), (realToFrac y2), (realToFrac z2)),
  cameraRot   = ((realToFrac x3), (realToFrac y3), (realToFrac z3)),
  cameraCenterAxis = (0, 0, 0),
  cameraYaw        = (0, 0, 0),
  cameraPitch      = (0, 0, 0),
  cameraRoll       = (0, 0, 0) }
  
main :: IO ()
main = do
	putStrLn "Running Test"
	let mainCamera = initCamera (0, 0.7, 0) (0,0,0) (0,0,0)
	mainCameraRef <- newIORef(mainCamera)
	mainCameraShowReadRef <- readIORef mainCameraRef
	putStrLn $ "==>" ++ show mainCamera
	putStrLn $ "==>" ++ show mainCameraShowReadRef
	
	{- Test to override the existing camera and print the data -}
	let mainCameraAnother = initCamera (0.2, 0.3, 4) (0,0,0) (0,0,0)
	writeIORef mainCameraRef mainCameraAnother
	
	mainCameraShowReadRef <- readIORef mainCameraRef
	putStrLn $ "==>" ++ show mainCameraShowReadRef
	
	putStrLn "Done"

Sign in to add a comment
Hosted by Google Code