En este manual se presentan ejemplos de las funciones de la librería de conjuntos Data.Vector 0.10.9.1. En los ejemplos se supone que se han importado la siguiente librería

ghci> import Data.Vector as V

1 El tipo de los vectores

ghci> :set +t
ghci> generate 5 (*2)
fromList [0,2,4,6,8]
it :: Vector Int
ghci> generate 5 (\i -> if even i then True else False)
fromList [True,False,True,False,True]
it :: Vector Bool

2 Funciones de acceso

2.1 Funciones de longitud

ghci> V.length (generate 5 (*2))
5
ghci> V.null (generate 0 (*2))
True
ghci> V.null (generate 5 (*2))
False

2.2 Índices

ghci> generate 5 (*2) ! 3
6
ghci> generate 5 (*2) ! 7
*** Exception: ./Data/Vector/Generic.hs:249 ((!)): index out of bounds (7,5)
ghci> generate 5 (*2) !? 3
Just 6
ghci> generate 5 (*2) !? 7
Nothing
ghci> V.head (generate 5 (*2))
0
ghci> V.last (generate 5 (*2))
8

2.3 Subvectores

ghci> slice 2 3 (generate 10 id)
fromList [2,3,4]
ghci> slice 2 3 (fromList [0..10])
fromList [2,3,4]
ghci> slice 9 3 (fromList [0..10])
fromList *** Exception: ./Data/Vector/Generic.hs:400 (slice): invalid slice (9,3,11)
ghci> V.init (generate 5 (*2))
fromList [0,2,4,6]
ghci> V.init (fromList [0..5])
fromList [0,1,2,3,4]
ghci> V.tail (generate 5 (*2))
fromList [2,4,6,8]
ghci> V.tail (fromList [0..5])
fromList [1,2,3,4,5]
ghci> V.take 3 (generate 5 (*2))
fromList [0,2,4]
ghci> V.take 3 (fromList [0..5])
fromList [0,1,2]
ghci> V.drop 3 (generate 5 (*2))
fromList [6,8]
ghci> V.drop 3 (fromList [0..5])
fromList [3,4,5]
ghci> V.splitAt 3 (generate 5 (*2))
(fromList [0,2,4],fromList [6,8])
ghci> V.splitAt 3 (fromList [0..5])
(fromList [0,1,2],fromList [3,4,5])

3 Constructores

3.1 Inicialización

ghci> empty
fromList []
ghci> singleton 5
fromList [5]
ghci> V.replicate 3 5
fromList [5,5,5]
ghci> generate 5 (*2)
fromList [0,2,4,6,8]
ghci> iterateN 5 (*2) 1
fromList [1,2,4,8,16]
ghci> iterateN 5 (+2) 0
fromList [0,2,4,6,8]

3.2 Desplegado

ghci> unfoldr (\n -> if n == 0 then Nothing else Just (2*n,n-1)) 5
fromList [10,8,6,4,2]
ghci> unfoldrN 3 (\n -> Just (2*n,n-1)) 10 
fromList [20,18,16]
ghci> constructN 3 V.length
fromList [0,1,2]
ghci> constructrN 3 V.length
fromList [2,1,0]

3.3 Enumeración

ghci> enumFromN 5 3
fromList [5,6,7]
ghci> enumFromStepN 1 2 5
fromList [1,3,5,7,9]
ghci> V.enumFromTo 2 7
fromList [2,3,4,5,6,7]
ghci> V.enumFromTo 'b' 'd'
fromList "bcd"
ghci> V.enumFromThenTo 1 4 10
fromList [1,4,7,10]

3.4 Concatenación

ghci> cons 7 (fromList [3,2,5])
fromList [7,3,2,5]
ghci> snoc (fromList [3,2,5]) 7
fromList [3,2,5,7]
ghci> (fromList [2,3]) V.++ (fromList [7,5,9])
fromList [2,3,7,5,9]
ghci> V.concat [fromList [2,3], fromList [5], fromList [3,7]]
fromList [2,3,5,3,7]

4 Modificaciones de vectores

4.1 Actualizaciones

ghci> fromList [3,5,2] // [(2,1),(0,7)]
fromList [7,5,1]
ghci> update (fromList [3,5,2]) (fromList [(2,1),(0,7)])
fromList [7,5,1]
ghci> update_ (fromList [3,5,2]) (fromList [2,0]) (fromList [1,7])
fromList [7,5,1]

4.2 Acumuladores

ghci> accum (+) (fromList [5,9,2]) [(2,1),(1,6),(0,3),(1,4)]
fromList [8,19,3]
ghci> accumulate (+) (fromList [5,9,2]) (fromList [(2,1),(1,6),(0,3),(1,4)])
fromList [8,19,3]
ghci> accumulate_ (+) (fromList [5,9,2]) (fromList [2,1,0,1]) (fromList [1,6,3,4])
fromList [8,19,3]

4.3 Permutaciones

ghci> V.reverse (fromList [3,2,5])
fromList [5,2,3]
ghci>  backpermute (fromList "abcd") (fromList [0,3,2,3,1,0])
fromList "adcdba"

5 Operaciones sobre pares de elementos

5.1 Indices

ghci> indexed (fromList [4,5,7])
fromList [(0,4),(1,5),(2,7)]

5.2 Aplicaciones

ghci> V.map (*2) (fromList [4,5,7])
fromList [8,10,14]
ghci> imap (*) (fromList [4,5,2,3])
fromList [0,5,4,9]
ghci> V.concatMap (\(i,x) -> V.replicate i x) (fromList [(3,'a'),(2,'c')])
fromList "aaacc"

5.3 Emparejamento

ghci> V.zip (fromList [3,2,5]) (fromList [1,0,7])
fromList [(3,1),(2,0),(5,7)]
ghci> V.zipWith (+) (fromList [3,2,5]) (fromList [4,7])
fromList [7,9]
ghci> V.zipWith (+) (fromList [3,2,5]) (fromList [4,7,9,6])
fromList [7,9,14]
ghci> let f x y z = Prelude.maximum [x,y,z]
ghci> izipWith f (fromList [3,0,2]) (fromList [4,0,1])
fromList [4,1,2]

5.4 Desemparejamiento

ghci> V.unzip (fromList [(3,1),(2,0),(5,7)])
(fromList [3,2,5],fromList [1,0,7])

6 Trabajando con predicados

6.1 Filtrado

ghci> V.filter (>3) (fromList [4,2,7,1,9,6])
fromList [4,7,9,6]
ghci> ifilter (\i x -> i+x < 9) (fromList [4,2,7,1,9,6])
fromList [4,2,1]
ghci> V.takeWhile (<9) (fromList [4,2,7,1,9,6])
fromList [4,2,7,1]
ghci> V.dropWhile (<9) (fromList [4,2,7,1,9,6])
fromList [9,6]

6.2 Particiones

ghci> partition (<5) (fromList [4,2,7,1,9,6])
(fromList [4,2,1],fromList [7,9,6])
ghci> unstablePartition (<5) (fromList [4,2,7,1,9,6])
(fromList [4,2,1],fromList [6,9,7])
ghci> V.span (<9) (fromList [4,2,7,1,9,6])
(fromList [4,2,7,1],fromList [9,6])
ghci> V.break (<9) (fromList [4,2,7,1,9,6])
(fromList [],fromList [4,2,7,1,9,6])

6.3 Búsqueda

ghci> V.elem 7 (fromList [4,2,7,1,9,6])
True
ghci> V.elem 5 (fromList [4,2,7,1,9,6])
False
ghci> V.notElem 7 (fromList [4,2,7,1,9,6])
False
ghci> V.notElem 5 (fromList [4,2,7,1,9,6])
True
ghci> V.find (>5) (fromList [4,2,7,1,9,6])
Just 7
ghci> V.find (>9) (fromList [4,2,7,1,9,6])
Nothing
ghci> V.findIndex (>5) (fromList [4,2,7,1,9,6])
Just 2
ghci> V.findIndex (>9) (fromList [4,2,7,1,9,6])
Nothing
ghci> V.findIndices (>5) (fromList [4,2,7,1,9,6])
fromList [2,4,5]
ghci> elemIndex 7 (fromList [4,2,7,1,9,6,7])
Just 2
ghci> elemIndex 5 (fromList [4,2,7,1,9,6,7])
Nothing
ghci> elemIndices 7 (fromList [4,2,7,1,9,6,7])
fromList [2,6]

6.4 Plegados

ghci> V.foldl (-) 9 (fromList [3,5])
1
ghci> (9-3)-5
1
ghci> V.foldl1 (-) (fromList [9,3,5])
1
ghci> (9-3)-5
1
ghci> V.foldr (-) 9 (fromList [3,5])
7
ghci> 3-(5-9)
7
ghci> V.foldr1 (-) (fromList [3,5,9])
7
ghci> 3-(5-9)
7
ghci> let f a i b = i*(a+b)
ghci> V.ifoldl f 9 (fromList [3,5,4])
18
ghci> f (f (f 9 0 3) 1 5) 2 4
18
ghci> let f a i b = i*(a+b)
ghci> V.ifoldr f 9 (fromList [3,5,4])
675
ghci> f 0 3 (f 1 5 (f 2 4 9))
675 

6.5 Plegados especiales

ghci> V.all (<7) (fromList [3,5,2])
True
ghci> V.all (<7) (fromList [3,9,2])
False 
ghci> V.any (<7) (fromList [3,9,2])
True
ghci> V.any (<1) (fromList [3,9,2])
False
ghci> V.and (fromList [True,2+3==5])
True
ghci> V.and (fromList [True,2+3/=5])
False
ghci> V.or (fromList [True,2+3/=5])
True
ghci> V.or (fromList [2==3,2+3/=5])
False
ghci> V.sum (fromList [3,5,2])
10
ghci> V.product (fromList [3,5,2])
30
ghci> V.maximum (fromList [3,5,2])
5
ghci> import Data.Ord
ghci> maximumBy (comparing abs) (fromList [3,-5,2])
-5
ghci> V.minimum (fromList [3,5,2])
2
ghci> import Data.Ord
ghci> minimumBy (comparing abs) (fromList [3,-5,2])
2
ghci> maxIndex (fromList [3,5,2])
1
ghci> import Data.Ord
ghci> maxIndexBy (comparing abs) (fromList [3,-5,2])
1
ghci> import Data.Ord
ghci> minIndex (fromList [3,-5,1])
1
ghci> import Data.Ord
ghci> minIndexBy (comparing abs) (fromList [3,-5,1])
2

6.6 Sumas de prefijos

ghci> prescanl (-) 9 (fromList [5,2,3,1])
fromList [9,4,2,-1]
ghci> [9,9-5,(9-5)-2,((9-5)-2)-3]
[9,4,2,-1]
ghci> postscanl (-) 9 (fromList [5,2,3,1])
fromList [4,2,-1,-2]
ghci> [9-5,(9-5)-2,((9-5)-2)-3,(((9-5)-2)-3)-1]
[4,2,-1,-2]
ghci> V.scanl (-) 9 (fromList [5,2,3,1])
fromList [9,4,2,-1,-2]
ghci> [9,9-5,(9-5)-2,((9-5)-2)-3,(((9-5)-2)-3)-1]
[9,4,2,-1,-2]
ghci> V.scanl1 (-) (fromList [9,5,2,3,1])
fromList [9,4,2,-1,-2]
ghci> [9,9-5,(9-5)-2,((9-5)-2)-3,(((9-5)-2)-3)-1]
[9,4,2,-1,-2]
ghci> V.prescanr (-) 9 (fromList [5,2,3,1])
fromList [-9,11,-8,9]
ghci> [2-(3-(1-9)),3-(1-9),1-9,9]
[-9,11,-8,9]
ghci> V.postscanr (-) 9 (fromList [5,2,3,1])
fromList [14,-9,11,-8]
ghci> [5-(2-(3-(1-9))),2-(3-(1-9)),3-(1-9),1-9]
[14,-9,11,-8]
ghci> V.scanr (-) 9 (fromList [5,2,3,1])
fromList [14,-9,11,-8,9]
ghci> [5-(2-(3-(1-9))),2-(3-(1-9)),3-(1-9),1-9,9]
[14,-9,11,-8,9]
ghci> V.scanr1 (-) (fromList [5,2,3,1,9])
fromList [14,-9,11,-8,9]
ghci> [5-(2-(3-(1-9))),2-(3-(1-9)),3-(1-9),1-9,9]
[14,-9,11,-8,9]

7 Conversiones

7.1 Listas

ghci> toList (fromList [3,2,5])
[3,2,5]
ghci> let v = fromList [3,2,5]
ghci> :t v
v :: Vector Integer
ghci> fromListN 5 [3..20]
fromList [3,4,5,6,7]

8 Referencias