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? Can anyone offer any insight? Patrick -- ===================== Patrick LeBoutillier Rosem?re, Qu?bec, Canada |
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 |
In reply to this post by Patrick LeBoutillier
Am Freitag 03 Juli 2009 17:58:22 schrieb Patrick LeBoutillier:
> 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? Exactly. You want banana :: (a -> (t -> b)) -> (t -> a) -> (t -> b) banana fun af = \tval -> ??? There's not much choice what you can do with those types and data. > Can anyone offer any insight? > > > Patrick |
Hi,
Thanks for the help. I figured it out after that. I'm having a hard time with the other exercises though, I'm currently stuck at 14: class Misty m where banana :: (a -> m b) -> m a -> m b unicorn :: a -> m a -- Exercise 14 -- Relative Difficulty: 6 moppy :: (Misty m) => [a] -> (a -> m b) -> m [b] moppy = error "todo" Does anyone know if the solutions are posted anywhere? Patrick On Fri, Jul 3, 2009 at 12:08 PM, Daniel Fischer<[hidden email]> wrote: > Am Freitag 03 Juli 2009 17:58:22 schrieb Patrick LeBoutillier: >> 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? > > Exactly. > > You want > > banana :: (a -> (t -> b)) -> (t -> a) -> (t -> b) > banana fun af = \tval -> ??? > > There's not much choice what you can do with those types and data. > >> Can anyone offer any insight? >> >> >> Patrick > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > -- ===================== Patrick LeBoutillier Rosem?re, Qu?bec, Canada |
Am Sonntag 05 Juli 2009 21:05:20 schrieb Patrick LeBoutillier:
> Hi, > > Thanks for the help. I figured it out after that. I'm having a hard > time with the other exercises though, I'm currently stuck at 14: > > > class Misty m where > banana :: (a -> m b) -> m a -> m b > unicorn :: a -> m a > > -- Exercise 14 > -- Relative Difficulty: 6 > moppy :: (Misty m) => [a] -> (a -> m b) -> m [b] > moppy = error "todo" moppy [] mop = ? moppy (a:as) mop = (mop a) ?? (moppy as mop) use (among other things) banana and unicorn to replace the question marks > > > Does anyone know if the solutions are posted anywhere? They're (under different names) in the standard libraries :) > > > Patrick |
Hi,
On Sun, Jul 5, 2009 at 3:44 PM, Daniel Fischer<[hidden email]> wrote: > Am Sonntag 05 Juli 2009 21:05:20 schrieb Patrick LeBoutillier: >> Hi, >> >> Thanks for the help. I figured it out after that. I'm having a hard >> time with the other exercises though, I'm currently stuck at 14: >> >> >> class Misty m where >> ? banana :: (a -> m b) -> m a -> m b >> ? unicorn :: a -> m a >> >> -- Exercise 14 >> -- Relative Difficulty: 6 >> moppy :: (Misty m) => [a] -> (a -> m b) -> m [b] >> moppy = error "todo" > > moppy [] mop = ? > moppy (a:as) mop = (mop a) ?? (moppy as mop) > > use (among other things) banana and unicorn to replace the question marks I came up with this:moppy [] mop = unicorn [] moppy (a:as) mop = banana (\b -> banana (\bs -> unicorn (b:bs)) (moppy as mop)) (mop a) moppy [] mop = unicorn [] moppy (a:as) mop = banana (\b -> banana (\bs -> unicorn (b:bs)) (moppy as mop)) (mop a) How do I make the second one nicer/shorter? Patrick > >> >> >> Does anyone know if the solutions are posted anywhere? > > They're (under different names) in the standard libraries :) I found it: forM > >> >> >> Patrick > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > -- ===================== Patrick LeBoutillier Rosem?re, Qu?bec, Canada |
On Mon, Jul 06, 2009 at 03:55:34PM -0400, Patrick LeBoutillier wrote:
> Hi, > > On Sun, Jul 5, 2009 at 3:44 PM, Daniel Fischer<[hidden email]> wrote: > > Am Sonntag 05 Juli 2009 21:05:20 schrieb Patrick LeBoutillier: > >> Hi, > >> > >> Thanks for the help. I figured it out after that. I'm having a hard > >> time with the other exercises though, I'm currently stuck at 14: > >> > >> > >> class Misty m where > >> ? banana :: (a -> m b) -> m a -> m b > >> ? unicorn :: a -> m a > >> > >> -- Exercise 14 > >> -- Relative Difficulty: 6 > >> moppy :: (Misty m) => [a] -> (a -> m b) -> m [b] > >> moppy = error "todo" > > > > moppy [] mop = ? > > moppy (a:as) mop = (mop a) ?? (moppy as mop) > > > > use (among other things) banana and unicorn to replace the question marks > > I came up with this:moppy [] mop = unicorn [] > moppy (a:as) mop = banana (\b -> banana (\bs -> unicorn (b:bs)) (moppy > as mop)) (mop a) > > > moppy [] mop = unicorn [] > moppy (a:as) mop = banana (\b -> banana (\bs -> unicorn (b:bs)) (moppy > as mop)) (mop a) > > How do I make the second one nicer/shorter? Great! You can't make it much shorter just using banana and unicorn. But you could make it a little nicer like so: ananab = flip banana moppy (a:as) mop = mop a `ananab` (\b -> moppy as mop `ananab` (\bs -> unicorn (b:bs)) That makes it a bit more obvious where the 'b' and 'bs' are coming from. You can also do something like this: liftBanana2 f mx my = mx `ananab` (\x -> my `ananab` (\y -> unicorn (f x y))) moppy (a:as) mop = liftBanana2 (:) (mop a) (moppy as mop) I'll let you figure out what 'ananab' and 'liftBanana2' are called in the standard libraries. =) -Brent |
Free forum by Nabble | Edit this page |