diff options
Diffstat (limited to 'Haskell-book/09/src/Zipping.purs')
| -rw-r--r-- | Haskell-book/09/src/Zipping.purs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Haskell-book/09/src/Zipping.purs b/Haskell-book/09/src/Zipping.purs new file mode 100644 index 0000000..32fe6da --- /dev/null +++ b/Haskell-book/09/src/Zipping.purs @@ -0,0 +1,27 @@ +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) |
