haskelllambdapattern-matchingzipwith

Simplify zipWith and case using LambdaCase


With the following zipWith expression:

zipWith3 (\foos bars bazs -> case (foos, bars, bazs) of
    (foo, bar, Just baz)  -> Right "hell yeah"
    (foo, bar, Nothing) -> Left "tough luck"
) ["foo1", "foo2"] ["bar1", "bar2"] [Just "baz1", Just "baz2"]

Is it possible to use LambdaCase to simplify the case expression with something like (doesn't compile):

zipWith3 (\case
    (foo, bar, Just baz)  -> Right "hell yeah"
    (foo, bar, Nothing) -> Left "tough luck"
) ["foo1", "foo2"] ["bar1", "bar2"] [Just "baz1", Just "baz2"]

In the first (working) version, case receives a tuple, but in the (failing) LambdaCase version, it seems that case would receive the three arguments instead of a tuple. I don't know if this is possible to do something like that.


Solution

  • You need to add curry3 (for example, from extra package):

    zipWith3 (curry3 $ \case
        (foo, bar, Just baz)  -> Right "hell yeah"
        (foo, bar, Nothing) -> Left "tough luck"
    ) ["foo1", "foo2"] ["bar1", "bar2"] [Just "baz1", Just "baz2"]