Haskell/Solutions/Understanding arrows

From Wikibooks, open books for an open world
< Haskell‎ | Solutions
Jump to: navigation, search

← Back to Understanding arrows


What is the type of the combined arrow?

The combined arrow has type a b d.


What is the type of the first robot?

first :: a b c -> a (b,d) (c,d)


  1. Write a function to swap two components of a tuple.
  2. Combine this helper function with the robots arr, (>>>) and first to implement the second robot
swap :: (a,b) -> (b,a)
swap (a,b) = (b,a)

second :: (Arrow a) => a b c -> a (d,b) (d,c)
second a = arr swap >>> first a >>> arr swap


  1. What is the type of the (***) robot?
  2. Given the (>>>), first and second robots, implement the (***) robot.
(***) :: a b d -> a c e -> a (b,c) (d,e)

f *** g = first f >>> second g


  1. Write a simple function to clone an input into a pair.
  2. Using your cloning function, as well as the robots arr, (>>>) and ***, implement the &&& robot
  3. Similarly, rewrite the following function without using &&&:
addA f g = f &&& g >>> arr (\ (y, z) -> y + z)
clone a = (a,a)

f &&& g = arr clone >>> f *** g

addA f g = arr clone >>> f *** g >>> arr (\(y,z) -> y + z)
-- OR
addA f g = arr clone >>> f *** g >>> arr (uncurry (+))

Arrow combinators (robots)[edit]

  1. Consider the charA parser from above. What would charA 'o' >>> charA 'n' >>> charA 'e' result in?
  2. Write a simplified version of that combined parser. That is: does it accept the empty string? What are its starting symbols? What is the dynamic parser for this?

FIXME: No solutions.