Say, we can write something like this:
zipWith (,) [1,2,3] [4,5,6]
If we want to tuple 3 list, we can write: zipWith3 (,,) [1,2,3] [4,5,6] [7,8,9]
We can also use zipWith4 (,,,) zipWith5(,,,,)
and so on.
Now, I want to do the same thing, but using adding instead comma operator. Is there any way to define it in the same terse way, not using lambdas like in
zipWith3 (\a b c -> a + b + c) [1, 2, 3] [4, 5, 6] [7, 8, 9]
Thanks in advance for any answer.
It sounds like you want "point free" style code for \a b c -> a + b + c
. Let it be known that, in general, \a b c -> a + b + c
is often preferable to point free code because it is much easier to read four weeks later when you found a bug.
There is a wiki article on point free programming (source).
You can also install the pointfree
package, which lets you solve these problems on the command line. For example,
$ pointfree '\x y z -> x + y + z' ((+) .) . (+)
So ((+) .) . (+)
is the point free version (x, y, and z are the "points", in case you were wondering, and no, this has nothing to do with geometry). You can use that definition if you'd like, but most people will look at your code and will have no idea what that funny looking piece of ASCII art is supposed to do. Half of them will work it out with pencil and paper, but isn't the original \x y z -> x + y + z
so much easier on the eyes?
Hint: If you ever need to figure out what some point free code does, look at the type:
Prelude> :t ((+) .) . (+) ((+) .) . (+) :: (Num a) => a -> a -> a -> a
Or you can install the pointful
package, which is approximately the inverse of pointfree
.
Summary: Welcome to the world of points free programming, proceed with caution lest your code be unreadable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With