<div dir="ltr"><div style="font-size:13px">Hello all,</div><div style="font-size:13px">I'm learning Haskell, and started to go through a set of intermediate exercises (<a href="https://www.fpcomplete.com/user/DanBurton/20-intermediate-exercises" target="_blank">https://www.fpcomplete.com/user/DanBurton/20-intermediate-exercises</a>). I am a bit puzzled about one of the exercises, and hope someone can help me understand why one of my solutions doesn't work.</div><div style="font-size:13px"><br></div><div style="font-size:13px">We have a typeclass, Misty (only the relevant banana function shown) as:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><br></div><div style="font-size:13px"><font face="monospace, monospace">class Misty m where</font></div><div style="font-size:13px"><font face="monospace, monospace">    banana :: (a -> m b) -> m a -> m b</font></div><div style="font-size:13px"><br></div><div style="font-size:13px"><br></div><div style="font-size:13px">The exercise asks to implement this typeclass for the type ‘((->) t)’. I started off by filling in the relevant types, and I get:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><font face="monospace, monospace">banana :: (a -> ((->) t b) ) -> ((->) t a) -> ((->) t b) </font></div><div style="font-size:13px"><font face="monospace, monospace">banana :: (a -> (t -> b)) -> (t -> a) -> (t -> b)</font></div><div style="font-size:13px"><br></div><div style="font-size:13px">Based on this, I decided to implement banana as:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><font face="monospace, monospace">banana f g = (\x -> f (g x))</font></div><div style="font-size:13px"><br></div><div style="font-size:13px">Here is my thought process:</div><div style="font-size:13px">- The type of f is ‘(a -> t -> b)’, and the type of g is ‘(t -> a)’</div><div style="font-size:13px">- g converts an argument of type ‘t’ into a result of type ‘a’.</div><div style="font-size:13px">- I then pass the result of ‘(g x)’ (which is of type ‘a’) as an argument to ‘f’.</div><div style="font-size:13px">- At this point, ‘f’ would be partially applied, and I *expect* to get a result of type ‘(t -> b)’</div><div style="font-size:13px"><br></div><div style="font-size:13px"><br></div><div style="font-size:13px">However, when I try to build my solution, I get the following error (code is in a file called intermediate-help.hs):</div><div style="font-size:13px"><br></div><div style="font-size:13px"><font face="monospace, monospace">[1 of 1] Compiling Main             ( intermediate-help.hs, interpreted )</font></div><div style="font-size:13px"><font face="monospace, monospace"><br></font></div><div style="font-size:13px"><font face="monospace, monospace">intermediate-help.hs:7:25:</font></div><div style="font-size:13px"><font face="monospace, monospace">    Couldn't match expected type ‘b’ with actual type ‘t -> b’</font></div><div style="font-size:13px"><font face="monospace, monospace">      ‘b’ is a rigid type variable bound by</font></div><div style="font-size:13px"><font face="monospace, monospace">          the type signature for</font></div><div style="font-size:13px"><font face="monospace, monospace">            banana :: (a -> t -> b) -> (t -> a) -> t -> b</font></div><div style="font-size:13px"><font face="monospace, monospace">          at intermediate-help.hs:7:5</font></div><div style="font-size:13px"><font face="monospace, monospace">    Relevant bindings include</font></div><div style="font-size:13px"><font face="monospace, monospace">      x :: t (bound at intermediate-help.hs:7:20)</font></div><div style="font-size:13px"><font face="monospace, monospace">      g :: t -> a (bound at intermediate-help.hs:7:14)</font></div><div style="font-size:13px"><font face="monospace, monospace">      f :: a -> t -> b (bound at intermediate-help.hs:7:12)</font></div><div style="font-size:13px"><font face="monospace, monospace">      banana :: (a -> t -> b) -> (t -> a) -> t -> b</font></div><div style="font-size:13px"><font face="monospace, monospace">        (bound at intermediate-help.hs:7:5)</font></div><div style="font-size:13px"><font face="monospace, monospace">    In the expression: f (g x)</font></div><div style="font-size:13px"><font face="monospace, monospace">    In the expression: (\ x -> f (g x))</font></div><div style="font-size:13px"><font face="monospace, monospace">Failed, modules loaded: none.</font></div><div style="font-size:13px"><br></div><div style="font-size:13px"><br></div><div style="font-size:13px"><br></div><div style="font-size:13px">So here's my confusion: The compiler is complaining that it cannot match expected type ‘b’ with actual type ‘t -> b’. However, as I reasoned above, when I wrote this code, I expected to get type ‘t -> b’. Clearly, my thought process has a hole, and I need help/advice from more experienced Haskellers to identify what I am missing.</div><div style="font-size:13px"><br></div><div style="font-size:13px">Thank you for any help,</div><div style="font-size:13px">~Umair</div></div>