Ad
  • Default User Avatar

    Simple example: bimap (+ 10) (* 13) (7, 3) = (17, 39)

    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
    
  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    It's all about "$". When there's enough dollars, work gets done.