[Haskell-cafe] Re: Generalizing zip

Jón Fairbairn jon.fairbairn at cl.cam.ac.uk
Thu Nov 16 13:44:43 EST 2006

"Jason Dagit" <dagit at eecs.oregonstate.edu> writes:

> Well, this is basically just a zip with a special base case.  But you
> can't just write it with zipWith because zipWith stops when it exausts
> either list.
> How about we define zipWith'' like this:
> zipWith'' _ []     _      l _ = [l]
> zipWith'' _ _      []     _ r = [r]
> zipWith'' f (x:xs) (y:ys) l r = f x y : zipWith'' f xs ys l r
> Then we can write:
> isPrefixOf xs ys = and (zipWith'' (==) xs ys True False)

I wonder if there is mileage to be had from persuing
something like this, rather different (off top of head, very
provisional, E&OE &c) approach:

   extend_infinitely l = map Just l ++ repeat Nothing
   promote1 rel (Just a) b = rel a b
   promote1 rel Nothing b = False
   is_pfx_of l1 l2 = and (zipWith (promote1 (==)) (extend_infinitely l2) l1)

? For at least the few minutes I've thought about it, it
seems like promote and extend_infinitely might be more
generally userful. I've probably missed something better,

> A point free reduction might look like the following and probably
> isn't worth it:
> isPrefixOf = (and .) . flip flip False . flip flip True . zipWith'' (==)


Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk

More information about the Haskell-Cafe mailing list