Home > OS >  Triple to pair point-free way
Triple to pair point-free way

Time:12-05

Here is a function that multiplies the first two elements of a three-elements tuple:

f (a, b, _) = a * b

I was curious if it was possible to make this function point-free. For a pair that would be as simple as f = uncurry . (*), and if we had something like tripleToPair, that would make the solution obvious, but that function is also written the most straightforward way, via pattern matching.

I asked the almighty pointfree.io, but it returned error 500. Is that even possible? The question is purely theoretical, in my opinion, the function is good as it is.

CodePudding user response:

The easiest way is with liftA2 (ab)using the Applicative instance for functions

import Control.Applicative

-- This two functions are defined in module Data.Tuple.Extra from extra package
-- You could use lenses too, I guess
fst3 (a,_,_) = a
snd3 (_,b,_) = b

f = liftA2 (*) fst3 snd3
  • Related