# Implementing “Pierre the Tight-rope” Walker from LYAH

Learn You a Haskell presents an excellent introduction to >>= with an example.

Basically, Pierre is a tight-rope walker that has Birds (Int) on each side of his tight-rope (represented by a tuple (Int, Int)). Birds can land on each side (only one side at a time) with addRight and addLeft. If the difference of birds on left and right is > 4, then Pierre falls.

LYAH says to use the Error Monad:

As an exercise, you can rewrite that with the error monad so that when the tightrope walker slips and falls, we remember how many birds were on each side of the pole when he fell.

Here's how I implemented it (but LYAH provided the type synonyms):

-- @author: LYAH
type Birds = Int
type Pole = (Birds, Birds)

addLeft :: Birds -> Pole -> Either Pole Pole
addLeft n (x, y) = let newLeft = x + n
newPole = (newLeft, y)
diff    = abs (newLeft - y)
in if (diff > 4) then Left  newPole
else               Right newPole

addRight :: Birds -> Pole -> Either Pole Pole
addRight n (x, y) = let newRight = y + n
newPole = (x, newRight)
diff    = abs (x - newRight)
in if (diff > 4) then Left  newPole
else               Right newPole

walkTightRope :: Either Pole Pole

walkTightRopeFailed :: Either Pole Pole

walkTightRopeDo :: Either Pole Pole
walkTightRopeDo = do
return p2


-I've found where bindings to read neater if I have more then 2 (personal choice).