
Untitled
By: a guest on
May 11th, 2012 | syntax:
None | size: 0.70 KB | hits: 11 | expires: Never
{-# LANGUAGE MonadComprehensions #-}
import Data.List.Ordered
import Data.Ord
import Control.Monad.Trans.Cont
fromList :: [a] -> Cont [Vec Double] a
fromList as = cont $ \k -> mergeAllBy (comparing norm) $ map k as
toList :: Cont [Vec Double] (Vec Double) -> [Vec Double]
toList = flip runCont return
genFromPair :: (Vec Double, Vec Double) -> [Vec Double]
genFromPair (e1, e2) = toList [x.*e1 + y.*e2 | x <- fromList [0..], y <- fromList [0..]]
data Vec a = Vec a a
deriving (Eq, Show)
instance Num a => Num (Vec a) where
(Vec x1 y1) + (Vec x2 y2) = Vec (x1+x2) (y1+y2)
-- ...
s .* (Vec x y) = Vec (s*x) (s*y)
norm (Vec x y) = sqrt (x^2 + y^2)
test = take 5 $ genFromPair (Vec 0 1, Vec 1 0)