diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-09 16:32:32 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-09 16:32:32 +0100 |
| commit | 3624c712d72d246f21d4e710cec7c11e052e0326 (patch) | |
| tree | f385cb51c72a0c5eeb2057609b75f5f8c6c4f272 /Haskell-book/09/src/Zipping.purs | |
| parent | c95abc31d62e296db4f1b537e3de440dd40defd1 (diff) | |
| download | book-exercises-3624c712d72d246f21d4e710cec7c11e052e0326.tar.gz | |
Add the haskell book
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) |
