add intercalate and concat

This commit is contained in:
rnhmjoj 2016-11-15 20:28:12 +01:00
parent ccf95e0cb9
commit c205db3ff7
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C

View File

@ -158,6 +158,14 @@ intersperse _ (x :- Nil) = x :- Nil
intersperse s (x :- xs) = gcastWith (double n) (x :- s :- intersperse s xs)
where n = sLength (x :- xs)
-- | 'intercalate' @xs xss@ is equivalent to
-- @('concat' ('intersperse' xs xss))@. It inserts the vector @xs@
-- in between the vectors in @xss@ and concatenates the result.
intercalate Vec a n Vec (Vec a n) m Vec a ((m :+ Pred m) :× n)
intercalate xs xss = concat (intersperse xs xss)
-- | 'foldl' applied to a binary operator, a starting value
-- and a 'Vec' reduces the vector to a single value obtained
-- by sequentially applying the operation from the left to the right.
@ -186,6 +194,16 @@ foldr₁ f (x :- Nil) = x
foldr f (x :- y :- ys) = f x (foldr f (y :- ys))
-- | The concatenation of all the elements of a vector of vectors.
concat Vec (Vec a n) m Vec a (m :× n)
concat Nil = Nil
concat (xs :- xss) = gcastWith proof (xs concat xss)
where
n = sLength xs
m = sLength xss
proof = plus_commut n (m %:× n)
-- | Applied to a singleton natural @n@ and a value produces
-- a vector obtained by duplicating the value @n@ times
replicate S n a Vec a n