[Haskell-beginners] Help with "20 intermediate haskell exercises"

Brent Yorgey byorgey at seas.upenn.edu
Fri Jul 3 12:08:31 EDT 2009


On Fri, Jul 03, 2009 at 11:58:22AM -0400, Patrick LeBoutillier wrote:
> Hi,
> 
> I'm running through these Haskell exercises:
> 
>   http://dibblego.wordpress.com/2008/09/18/20-intermediate-haskell-exercises/
> 
> and I'm stuck at number 9:
> 
> 
> class Misty m where
>   banana :: (a -> m b) -> m a -> m b
>   unicorn :: a -> m a
> 
> -- Exercise 9
> -- Relative Difficulty: 6
> instance Misty ((->) t) where
>   banana = ???
>   unicorn x = (\t -> x)
> 
> 
> I can picture it when m is "[]" or "Maybe", but I can't wrap my head
> around the banane implementation for "((->) t)".
> I can see that this somewhat looks like a Monad, with unicorn = return
> and banana = (flip >>=) or something. Perhaps some kind of reader
> Monad?

Precisely. ((->) t) is the reader monad.

Just follow the types!

  banana :: (a -> m b) -> m a -> m b

substituting ((->) t) for m (remembering that (->) associates to the
right):

  banana :: (a -> t -> b) -> (t -> a) -> t -> b
  banana f g t = ?

Can you figure out how to apply f :: (a -> t -> b) and g :: (t -> a)
to a 't' in order to get a 'b'?

-Brent


More information about the Beginners mailing list