So, (bimap f g (a,b)) applies f to a, and g to b, while maintaining the tuple structure.
Another example, but with Either:
bimap (+ 10) (* 13) (Left 9) = Left 19
bimap (+ 10) (* 13) (Right 2) = Right 26
Now, a clever trick is to apply join to bimap. It happens to be the case that join bimap applies the same function to both parts of a tuple:
join bimap (+2) (2,3) = (4,5)
The reason this works is due to the definition of join and the monad instance for functions (i.e. (->) r):
-- definitions
join x := x >>= id
f >>= g := \r -> g (f r) r -- (for functions only)
-- therefore:
join f = f >>= id
= \r -> id (f r) r
= \r -> f r r
-- specific to bimap:
join bimap = \r -> bimap r r
This comment is hidden because it contains spoiler information about the solution
Simple example:
bimap (+ 10) (* 13) (7, 3) = (17, 39)
So,
(bimap f g (a,b))
appliesf
toa
, andg
tob
, while maintaining the tuple structure.Another example, but with
Either
:Now, a clever trick is to apply
join
tobimap
. It happens to be the case thatjoin bimap
applies the same function to both parts of a tuple:The reason this works is due to the definition of
join
and the monad instance for functions (i.e.(->) r
):what is bimap?
This comment is hidden because it contains spoiler information about the solution
It's all about "$". When there's enough dollars, work gets done.
how does it work?