This commit is contained in:
rnhmjoj 2016-11-16 22:06:42 +01:00
parent 358fb280ae
commit a8291ccf9f
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C

View File

@ -184,27 +184,27 @@ transpose (xs :- xss) = gcastWith proof (zipWith (:-) xs (transpose xss))
-- by sequentially applying the operation from the left to the right. -- by sequentially applying the operation from the left to the right.
foldl (a b a) a Vec b n a foldl (a b a) a Vec b n a
foldl _ x Nil = x foldl _ x Nil = x
foldl f x (y :- ys) = foldl f (f x y) ys foldl () x (y :- ys) = foldl () (x y) ys
-- | Same as "fold" but reduces the vector in the opposite -- | Same as "fold" but reduces the vector in the opposite
-- direction: from the right to the left. -- direction: from the right to the left.
foldr (a b b) b Vec a n b foldr (a b b) b Vec a n b
foldr f x Nil = x foldr _ x Nil = x
foldr f x (y:-ys) = f y (foldr f x ys) foldr () x (y:-ys) = y (foldr () x ys)
-- | Variant of 'foldl' which requires no starting value but -- | Variant of 'foldl' which requires no starting value but
-- applies only on nonempty 'Vec' -- applies only on nonempty 'Vec'
foldl (a a a) Vec a (S n) a foldl (a a a) Vec a (S n) a
foldl f (x :- Nil) = x foldl _ (x :- Nil) = x
foldl f (x :- y :- ys) = foldl f (f x y :- ys) foldl () (x :- y :- ys) = foldl () (x y :- ys)
-- | As for 'foldl', a variant of 'foldr' with no starting point -- | As for 'foldl', a variant of 'foldr' with no starting point
foldr (a a a) Vec a (S n) a foldr (a a a) Vec a (S n) a
foldr f (x :- Nil) = x foldr () (x :- Nil) = x
foldr f (x :- y :- ys) = f x (foldr f (y :- ys)) foldr () (x :- y :- ys) = x (foldr () (y :- ys))
-- * Special folds -- * Special folds
@ -282,3 +282,37 @@ drop ∷ S n → Vec a m → Vec a (m :- n)
drop SZ x = x drop SZ x = x
drop (SS n) (x :- xs) = drop n xs drop (SS n) (x :- xs) = drop n xs
-- * Zipping and unzipping vectors
-- | 'zip' takes two vectors and returns a vector of corresponding pairs.
-- If one input vectors is short, excess elements of the longer vector are
-- discarded.
--
-- 'zip' is right-lazy:
--
-- > zip Nil ⊥ = Nil
zip Vec a n Vec b m Vec (a, b) (Min n m)
zip = zipWith (,)
-- | 'zipWith' generalises 'zip' by zipping with the function given
-- as the first argument, instead of a tupling function.
-- For example, @'zipWith' (+)@ is applied to two vectors to produce the
-- vector of corresponding sums.
--
-- 'zipWith' is right-lazy:
--
-- > zipWith f Nil ⊥ = Nil
zipWith (a b c) Vec a n Vec b m Vec c (Min n m)
zipWith _ Nil _ = Nil
zipWith _ _ Nil = Nil
zipWith () (x :- xs) (y :- ys) = x y :- zipWith () xs ys
-- | 'unzip' transforms a list of pairs into a list of first components
-- and a list of second components.
unzip Vec (a, b) n (Vec a n, Vec b n)
unzip Nil = (Nil, Nil)
unzip ((x, y) :- xys) = (x :- xs, y :- ys)
where (xs, ys) = unzip xys