Scrapbook-0.4.0: code examples
Copyright© Frank Jung 2020
LicenseGPL-3
Safe HaskellSafe-Inferred
LanguageHaskell2010

ZipFold

Description

From Zipping with Folds which is based on How to Zip with Folds.

The Preludes version of zip uses recursion:

zip :: [a] -> [b] -> [(a,b)]
zip []     _bs    = []
zip _as    []     = []
zip (a:as) (b:bs) = (a,b) : zip as bs

But, this won't work for streams. The version included below does work with streams. It uses a recursive type to represent the stream data. This matches from the beginning of the stream. You could match from the end of the stream by swapping foldr with foldl.

Synopsis
  • newtype Zip a b = Zip {}
  • zip :: [a] -> [b] -> [(a, b)]
  • zip' :: [a] -> [b] -> [(a, b)]

Documentation

newtype Zip a b Source #

Defines a recursive type.

Constructors

Zip 

Fields

zip :: [a] -> [b] -> [(a, b)] Source #

Function to fuse two folds.

zip' :: [a] -> [b] -> [(a, b)] Source #

Zip from fold.

>>> zip' [1,2,3] [4,5,6]
[(1,4),(2,5),(3,6)]
>>> zip' "abc" []
[]
>>> zip' [] "abc"
[]