summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-28 07:59:31 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-28 07:59:31 +0000
commitfbdb848b11f967bd23d4c4d1d9283e71cb834633 (patch)
tree1f0e969d308a9d3e4501f62d984fafd6bd3d5a63 /examples
parentdbd943b89ff481e0971f86c2271223cfddee7a02 (diff)
move successive to Tests and examples
Diffstat (limited to 'examples')
-rw-r--r--examples/monadic.hs40
1 files changed, 34 insertions, 6 deletions
diff --git a/examples/monadic.hs b/examples/monadic.hs
index b933de9..7c6e0f4 100644
--- a/examples/monadic.hs
+++ b/examples/monadic.hs
@@ -1,18 +1,17 @@
1-- monadic computations 1-- monadic computations
2-- (contributed by Vivian McPhail) 2-- (contributed by Vivian McPhail)
3 3
4import Numeric.Container 4import Numeric.LinearAlgebra
5import Control.Monad.State 5import Control.Monad.State.Strict
6import Control.Monad.Maybe 6import Control.Monad.Maybe
7import Foreign.Storable(Storable)
8import System.Random(randomIO)
7 9
8------------------------------------------- 10-------------------------------------------
9 11
10-- an instance of MonadIO, a monad transformer 12-- an instance of MonadIO, a monad transformer
11type VectorMonadT = StateT Int IO 13type VectorMonadT = StateT Int IO
12 14
13v :: Vector Int
14v = 10 |> [0..]
15
16test1 :: Vector Int -> IO (Vector Int) 15test1 :: Vector Int -> IO (Vector Int)
17test1 = mapVectorM $ \x -> do 16test1 = mapVectorM $ \x -> do
18 putStr $ (show x) ++ " " 17 putStr $ (show x) ++ " "
@@ -69,10 +68,34 @@ isMonotoneIncreasing v =
69 Nothing -> False 68 Nothing -> False
70 Just _ -> True 69 Just _ -> True
71 70
72w = fromList ([1..10]++[10,9..1]) :: Vector Double
73 71
74------------------------------------------- 72-------------------------------------------
75 73
74-- | apply a test to successive elements of a vector, evaluates to true iff test passes for all pairs
75successive_ :: Storable a => (a -> a -> Bool) -> Vector a -> Bool
76successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ step (subVector 1 (dim v - 1) v))) (v @> 0)
77 where step e = do
78 ep <- lift $ get
79 if t e ep
80 then lift $ put e
81 else (fail "successive_ test failed")
82
83-- | operate on successive elements of a vector and return the resulting vector, whose length 1 less than that of the input
84successive :: (Storable a, Storable b) => (a -> a -> b) -> Vector a -> Vector b
85successive f v = evalState (mapVectorM step (subVector 1 (dim v - 1) v)) (v @> 0)
86 where step e = do
87 ep <- get
88 put e
89 return $ f ep e
90
91-------------------------------------------
92
93v :: Vector Int
94v = 10 |> [0..]
95
96w = fromList ([1..10]++[10,9..1]) :: Vector Double
97
98
76main = do 99main = do
77 v' <- test1 v 100 v' <- test1 v
78 putStrLn "" 101 putStrLn ""
@@ -84,7 +107,12 @@ main = do
84 putStrLn "" 107 putStrLn ""
85 evalStateT (indexPlusSum v) 0 108 evalStateT (indexPlusSum v) 0
86 putStrLn "-----------------------" 109 putStrLn "-----------------------"
110 mapVectorM_ print v
111 print =<< (mapVectorM (const randomIO) v :: IO (Vector Double))
112 print =<< (mapVectorM (\a -> fmap (+a) randomIO) (5|>[0,100..1000]) :: IO (Vector Double))
113 putStrLn "-----------------------"
87 print $ isMonotoneIncreasing w 114 print $ isMonotoneIncreasing w
88 print $ isMonotoneIncreasing (subVector 0 7 w) 115 print $ isMonotoneIncreasing (subVector 0 7 w)
89 print $ successive_ (>) v 116 print $ successive_ (>) v
90 print $ successive_ (>) w 117 print $ successive_ (>) w
118 print $ successive (+) v