Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.70 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. {-# LANGUAGE MonadComprehensions #-}
  2.  
  3. import Data.List.Ordered
  4. import Data.Ord
  5. import Control.Monad.Trans.Cont
  6.  
  7. fromList :: [a] -> Cont [Vec Double] a
  8. fromList as = cont $ \k -> mergeAllBy (comparing norm) $ map k as
  9.  
  10. toList :: Cont [Vec Double] (Vec Double) -> [Vec Double]
  11. toList = flip runCont return
  12.  
  13. genFromPair :: (Vec Double, Vec Double) -> [Vec Double]
  14. genFromPair (e1, e2) = toList [x.*e1 + y.*e2 | x <- fromList [0..], y <- fromList [0..]]
  15.  
  16. data Vec a = Vec a a
  17.     deriving (Eq, Show)
  18.  
  19. instance Num a => Num (Vec a) where
  20.     (Vec x1 y1) + (Vec x2 y2) = Vec (x1+x2) (y1+y2)
  21.     -- ...
  22.  
  23. s .* (Vec x y) = Vec (s*x) (s*y)    
  24. norm (Vec x y) = sqrt (x^2 + y^2)
  25.  
  26. test = take 5 $ genFromPair (Vec 0 1, Vec 1 0)