module Zipping where import Data.Array (head, tail, (:)) import Data.Maybe (Maybe(..)) import Data.Tuple (Tuple(..)) -- 1 zip :: forall a b. Array a -> Array b -> Array (Tuple a b) zip a b = f (head a) (head b) (tail a) (tail b) where f Nothing _ _ _ = [] f _ Nothing _ _ = [] f _ _ Nothing _ = [] f _ _ _ Nothing = [] f (Just x) (Just y) (Just xs) (Just ys) = (Tuple x y) : (f (head xs) (head ys) (tail xs) (tail ys)) -- 2 zipWith :: forall a b c. (a -> b -> c) -> Array a -> Array b -> Array c zipWith f a b = f' (head a) (head b) (tail a) (tail b) where f' Nothing _ _ _ = [] f' _ Nothing _ _ = [] f' _ _ Nothing _ = [] f' _ _ _ Nothing = [] f' (Just x) (Just y) (Just xs) (Just ys) = (f x y) : (f' (head xs) (head ys) (tail xs) (tail ys)) -- 3 zip' :: forall a b. Array a -> Array b -> Array (Tuple a b) zip' = zipWith (\a b -> Tuple a b)