summaryrefslogtreecommitdiff
path: root/examples/vector-map.hs
blob: d778358686777561e73022f95d0c17b715cd3f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- use of vectorMapM
--

-------------------------------------------

import Data.Packed.Vector
import Numeric.LinearAlgebra.Interface

import Control.Monad.State
import Control.Monad.Trans

-------------------------------------------

-- an instance of MonadIO, a monad transformer
type VectorMonadT = StateT Int IO

v :: Vector Int
v = fromList $ take 10 [0..]

test1 :: Vector Int -> IO (Vector Int)
test1 = do
        mapVectorM (\x -> do
                          putStr $ (show) x ++ " "
                          return (x + 1))
                            
-- we can have an arbitrary monad AND do IO
addInitialM :: Vector Int -> VectorMonadT ()
addInitialM = mapVectorM_ (\x -> do
                                 i <- get
                                 liftIO $ putStr $ (show $ x + i) ++ " "
                                 put $ x + i
                          )

-------------------------------------------
main = do
       v' <- test1 v
       putStrLn ""
       putStrLn $ show v'
       evalStateT (addInitialM v) 1
       putStrLn ""
       return ()