2

I wanted a function in the style of zip, but rather than stopping when one of the supplied lists runs out, I wanted it to continue using zip on the rest of the lists.

Looking around at a few Emacs functions (referring mainly to those internal to Emacs and in the Common Lisp, and dash.el extensions), I found a few functions like mapcar*, -zip, and -zip-fill that were close to what I was looking for, but ended when one of the given lists ran out.

I ended up writing my own function, but I'm interested in knowing why many of the functions I saw implement this behavior. Looking online, it appears that this is the most common interpretation of how zip should work.

Would my desired function be considered zip? I figure it may be called something else, unless is a specialized unnamed variant.

To hopefully clarify myself some, here is the function I wrote:

(defun my-zip-alt (&rest lists)
  (when lists
    (cons (mapcar #'car lists)
          (apply #'my-zip-alt (delq nil (mapcar #'cdr lists))))))

;; (my-zip-alt '(1 2 3) '(4 5) '(6) '(nil))
;;=> ((1 4 6 nil) (2 5) (3))

Recommendations for making my function simpler or more efficient are welcome, of course :)

1 Answers1

4

Perhaps, they could be called align or alignWith, as in the these Haskell package.

alignWith takes a function which decides how to combine all the variants of the non-uniform tuples (which arise after you continue when the shortest list ran out):

align :: f a -> f b -> f (These a b)
alignWith :: (These a b -> c) -> f a -> f b -> f c
data These a b = This a | That b | These a b

(Here, f is to be understood as a functor type constructor, like "list". A list of a is a special case of f a.)

(Sorry for the answer being in Haskell! but if we are looking for commonly used names for such functions, it shouldn't matter much.)


I've also seen another Haskell package implementing the same idea.

In Apart from "these" package, who else implements zipping that continues after the shortest list in a library?, the other implementation under the name zipOr has been brought up.


And the version "as in F#" in the post online you have linked has been called pair once in a Haskell library: A safe version of zip which will fail (return Nothing) on uneven length lists.

  • Thanks for the response, and good observation! The function names with `align` seem kinda odd to me, and I can see why they didn't come up in my research (Emacs "alignment" usually concerns text spacing within the buffer). Personally, I prefer a name more like `zip-continue` or something for Emacs, but the Haskell you provided gives me more info to consider. –  Jul 03 '15 at 15:33