From Wikibooks, the open-content textbooks collection
← Back to Understanding arrows
| Exercises |
| What is the type of the combined arrow? |
The combined arrow has type a b d.
| Exercises |
What is the type of the first robot? |
first :: a b c -> a (b,d) (c,d)
[edit] second
| Exercises |
- Write a function to swap two components of a tuple.
- Combine this helper function with the robots
arr, (>>>) and first to implement the second robot
|
swap (a,b) = (b,a)
second :: (Arrow a) => a b c -> a (d,b) (d,c)
second a = arr swap >>> first a >>> arr swap
| Exercises |
- What is the type of the
(***) robot?
- 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
| Exercises |
- Write a simple function to clone an input into a pair.
- Using your cloning function, as well as the robots
arr, (>>>) and ***, implement the &&& robot
- 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 (+))
[edit] Arrow combinators (robots)
| Exercises |
- Consider the
charA parser from above. What would charA 'o' >>> charA 'n' >>> charA 'e' result in?
- 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.