In message <[EMAIL PROTECTED]>
          Gisle Aas <[EMAIL PROTECTED]> wrote:

> The upcoming Python (v2.0) introduces a builtin called zip() that does
> the same thing:
>
>   for a,b,c in zip(aa,bb,cc):
>       ...
>
> There are also question on how long the resulting list should be if
> @a, @b, @c is not of the same length.  I think zip() was defined to
> stop when the first list runs out.

I believe zip is quite a common name for this operation in
functional languages. Certainly Miranda uses it to turn a
tuple of lists into a list of tuples, stopping as soon as
any of the lists runs out.

> Python also has a map function that can take multiple lists and iterate
> over them in sync.  E.g.
>
>   map(func, list1, list2, list3)
>
> where 'func' must be a function taking 3 arguments.  Can't see how to
> easily extend perl's map in that way.  Perhaps we could introduce
> map2, map3,... builtins? :-)

This is what Miranda calls zipwith. Combined with reduce
you can do things like scalar products very simply:

  reduce(plus, zipwith(multiply, list1, list2))

Note that Miranda actually calls reduce fold though - well
actually foldl or foldr depending on which end of the list
you start at.

Tom

-- 
Tom Hughes ([EMAIL PROTECTED])
http://www.compton.nu/
...Hacking's just another word for nothing left to kludge.

Reply via email to