Programming Challenges

Let's solve coding challenges to practice common computation patterns.

Challenge 1: map on either type

The following algebraic datatype defines the either type:

type ('a, 'b) either = Left of 'a | Right of 'b

As a convention, Right of 'b holds the correct value while Left of 'a represents an error value.

Write a mapping function for either called map_either : ('a -> 'b) -> ('c, 'a) either -> ('c, 'b) either.

Examples: ...