<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Gotcha.<div class=""><br class=""></div><div class="">So, maximumBy takes a function as it’s first argument, and a foldable as it’s second argument. If you’re not sure what a foldable is yet, just know it’s something you can fold over, or iterate <span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">over</span>, or loop through (whichever phrasing you feel most comfortable with based on languages you’ve previously used). There’s more to it, but you’ll get into that later. Examples of something foldable would be lists or trees.</div><div class=""><br class=""></div><div class="">If you look at the type signature of the function you are passing to maximumBy as it’s first argument (‘compare’), you’ll see the class constraint of Ord for whichever data type ‘a’ you pass to the first and second arguments of the ’compare’ function. </div><div class=""><br class=""></div><div class=""><div class="">Prelude Data.List> :t compare</div><div class="">compare :: Ord a => a -> a -> Ordering</div></div><div class=""><br class=""></div><div class="">Any data type which is an instance of the Ord type class implements the following functions, including ‘compare’.</div><div class=""><br class=""></div><div class=""><div class="">Prelude Data.List> :i Ord</div><div class="">class Eq a => Ord a where</div><div class="">  <font color="#ffff00" class="">compare :: a -> a -> Ordering</font></div><div class="">  (<) :: a -> a -> Bool</div><div class="">  (<=) :: a -> a -> Bool</div><div class="">  (>) :: a -> a -> Bool</div><div class="">  (>=) :: a -> a -> Bool</div><div class="">  max :: a -> a -> a</div><div class="">  min :: a -> a -> a</div></div><div class="">… more info elided</div><div class=""><br class=""></div><div class="">So, the types Int or Float, for example, both have instances of the Ord type class. This means you can take 2 Ints and pass them to compare and you’ll get back an Ordering (Ordering is its own data type).</div><div class=""><br class=""></div><div class=""><div class="">Prelude Data.List> compare 1 2</div><div class="">LT   </div><div class="">Prelude Data.List> compare 2 2</div><div class="">EQ  </div><div class="">Prelude Data.List> compare 3 2</div><div class="">GT  </div><div class="">Prelude Data.List> :i Ordering</div><div class="">data Ordering = LT | EQ | GT    -- Defined in ‘GHC.Types’</div></div><div class=""><br class=""></div><div class="">LT stands for Less Than, EQ for Equal, and GT for Greater Than. So, maximumBy takes a foldable (let’s say a list, for example) and uses the compare function on 2 instances of the data type held in the list at a time. Each time the result of a comparison is GT, it takes the value that produced that GT result and keeps comparing that value to the others in the list until it finds another value which produces GT or it reaches the end of the list. It then gives you back the ‘greatest’ value it found after ‘folding’ over the list. Exactly how this comparison is done in any more detail relies on you understanding folding and foldables.</div><div class=""><br class=""></div><div class="">I hope this helped and didn’t just make it worse by being too verbose. Lemme know if you’ve got it now, or if the actual details of how folding is carried out is what’s tripping you up.</div><div class=""><br class=""></div><div class=""><br class=""><div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Best regards,</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Daniel van de Ghinste</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div></div></div><div><br class=""><blockquote type="cite" class=""><div class="">On 22 May 2020, at 20:52, Alexander Chen <<a href="mailto:alexander@chenjia.nl" class="">alexander@chenjia.nl</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family:Arial; font-size:13px;" class="">Hi,<div class=""><br class=""></div><div class="">maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a</div><div class=""><br class=""></div><div class="">its in the Data.List<br class=""><br class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">May 22, 2020 8:33:33 PM CEST "Daniel van de Ghinste (Lord_Luvat)" <<a href="mailto:danielvandeghinste@gmail.com" class="">danielvandeghinste@gmail.com</a>> wrote:<blockquote type="cite" cite="x-msg://6/%3C95B07D1E-14B7-46C1-9764-15D624F783D7@gmail.com%3E" class="">Hi,<div class=""><br class=""></div><div class="">Maybe I’m on a different version, but I don’t see a function called maximumBy in my base Prelude. Can you give us a type signature for the function? If you’re not sure how to do that just type:</div><div class="">Prelude> :t maximumBy</div><div class="">In your ghci interpreter and it should return the type signature of whatever you have after ‘:t ‘ (this works for compound expressions too if you put them in brackets)</div><div class=""><br class=""></div><div class="">Perhaps maximumBy is what you’re meant to call your rewrite of the existing function I see called ‘maximum’ (seems to do the same thing). Let me know if thats the case and I can explain how it works.</div><div class=""><br class=""></div><div class=""><br class=""><div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Best regards,</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Daniel van de Ghinste</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></div></div><div class=""><blockquote type="cite" class=""><div class="">On 22 May 2020, at 20:20, Alexander Chen <<a href="mailto:alexander@chenjia.nl" class="">alexander@chenjia.nl</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family:Arial; font-size:13px;" class="">Hi,<div class=""><br class=""></div><div class="">I want to re-write a function maximumBy (its an assignment).</div><div class=""><br class=""></div><div class="">However, I don't get how it works.</div><div class=""><br class=""></div><div class="">>maximumBy compare [1,53,9001, 10]</div><div class="">9001</div><div class=""><br class=""></div><div class="">but what does it actually do to get there? </div><div class=""><br class=""></div><div class="">thanks in advance.</div><div class=""><br class=""></div><div class="">best,<br class=""><br class=""><div class=""><div style="font-family:Arial; font-size:13px;" class=""><br class=""></div></div></div></div>_______________________________________________<br class="">Beginners mailing list<br class=""><a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br class=""><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div><br class=""><br class=""><div class=""><div style="font-family:Arial; font-size:13px;" class=""><br class=""></div></div></div></div></div></blockquote></div><br class=""></div></body></html>