<div dir="ltr">Here's an example that may help:<div><br></div><div><font face="monospace">> ((==) <*> reverse) "radar"</font></div><div><font face="monospace">True</font></div><div><br></div><div>Using the definition for <font face="monospace">(<*>)</font>, we have<font face="monospace"> f = (==) and</font> <font face="monospace">g = reverse</font>, and this becomes:</div><div><br></div><div><font face="monospace">(==) "radar" (reverse "radar")</font></div><div><br></div><div>Or, once we make <font face="monospace">(==)</font> infix,</div><div><br></div><div><font face="monospace">"radar" == reverse "radar"</font></div><div><br></div><div>So we might say:</div><div><br></div><div><font face="monospace">isPalindome x = ((==) <*> reverse) x</font></div><div><br></div><div>And we can <font size="2"><i>eta reduce</i> that to get</font></div><div><font size="2"><br></font></div><div><font size="2" face="monospace">isPalindrome = (==) <*> reverse</font></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Aug 22, 2016 at 4:54 PM Theodore Lief Gannon <<a href="mailto:tanuki@gmail.com">tanuki@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Yes, (g x) is the second argument to f. Consider the type signature:<br><div><br></div><div>(<*>) :: Applicative f => f (a -> b) -> f a -> f b</div><div><br></div><div>In this case, the type of f is ((->) r). Specialized to that type:</div><div><br></div><div>(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)</div></div><div dir="ltr"><div>f <*> g = \x -> f x (g x)</div><div><br></div></div><div dir="ltr"><div>Breaking down the pieces...</div><div>f :: r -> a -> b</div><div>g :: r -> a</div><div>x :: r<br></div><div>(g x) :: a</div><div>(f x (g x)) :: b</div><div><br></div><div>The example is made a bit confusing by tossing in an fmap. As far as the definition above is concerned, 'f' in the example is ((+) <$> (+3)) and that has to be resolved before looking at <*>.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 22, 2016 at 9:07 AM, Olumide <span dir="ltr"><<a href="mailto:50295@web.de" target="_blank">50295@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi List,<br>
<br>
I'm struggling to relate the definition of a function as a function<br>
<br>
instance Applicative ((->) r) where<br>
pure x = (\_ -> x)<br>
f <*> g = \x -> f x (g x)<br>
<br>
with the following expression<br>
<br>
ghci> :t (+) <$> (+3) <*> (*100)<br>
(+) <$> (+3) <*> (*100) :: (Num a) => a -> a<br>
ghci> (+) <$> (+3) <*> (*100) $ 5<br>
508<br>
<br>
>From chapter 11 of LYH <a href="http://goo.gl/7kl2TM" rel="noreferrer" target="_blank">http://goo.gl/7kl2TM</a> .<br>
<br>
I understand the explanation in the book: "we're making a function that will use + on the results of (+3) and (*100) and return that. To demonstrate on a real example, when we did (+) <$> (+3) <*> (*100) $ 5, the 5 first got applied to (+3) and (*100), resulting in 8 and 500. Then, + gets called with 8 and 500, resulting in 508."<br>
<br>
The problem is that I can't relate that explanation with the definition of a function as an applicative; especially f <*> g = \x -> f x (g x) . Is (g x) the second argument to f?<br>
<br>
Regards,<br>
<br>
- Olumide<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>