diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-09-28 07:59:31 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-09-28 07:59:31 +0000 |
commit | fbdb848b11f967bd23d4c4d1d9283e71cb834633 (patch) | |
tree | 1f0e969d308a9d3e4501f62d984fafd6bd3d5a63 /examples | |
parent | dbd943b89ff481e0971f86c2271223cfddee7a02 (diff) |
move successive to Tests and examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/monadic.hs | 40 |
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 | ||
4 | import Numeric.Container | 4 | import Numeric.LinearAlgebra |
5 | import Control.Monad.State | 5 | import Control.Monad.State.Strict |
6 | import Control.Monad.Maybe | 6 | import Control.Monad.Maybe |
7 | import Foreign.Storable(Storable) | ||
8 | import 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 |
11 | type VectorMonadT = StateT Int IO | 13 | type VectorMonadT = StateT Int IO |
12 | 14 | ||
13 | v :: Vector Int | ||
14 | v = 10 |> [0..] | ||
15 | |||
16 | test1 :: Vector Int -> IO (Vector Int) | 15 | test1 :: Vector Int -> IO (Vector Int) |
17 | test1 = mapVectorM $ \x -> do | 16 | test1 = 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 | ||
72 | w = 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 | ||
75 | successive_ :: Storable a => (a -> a -> Bool) -> Vector a -> Bool | ||
76 | successive_ 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 | ||
84 | successive :: (Storable a, Storable b) => (a -> a -> b) -> Vector a -> Vector b | ||
85 | successive 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 | |||
93 | v :: Vector Int | ||
94 | v = 10 |> [0..] | ||
95 | |||
96 | w = fromList ([1..10]++[10,9..1]) :: Vector Double | ||
97 | |||
98 | |||
76 | main = do | 99 | main = 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 | ||