Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  MaximumBy (Daniel van de Ghinste (Lord_Luvat))
   2. Re:  MaximumBy (Alexander Chen) (nowsnow)


----------------------------------------------------------------------

Message: 1
Date: Fri, 22 May 2020 21:28:05 +0200
From: "Daniel van de Ghinste (Lord_Luvat)"
        <danielvandeghin...@gmail.com>
To: Alexander Chen <alexan...@chenjia.nl>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] MaximumBy
Message-ID: <7fee92d8-7e5c-4ba7-9d41-f0930c795...@gmail.com>
Content-Type: text/plain; charset="utf-8"

Gotcha.

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 over, 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.

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. 

Prelude Data.List> :t compare
compare :: Ord a => a -> a -> Ordering

Any data type which is an instance of the Ord type class implements the 
following functions, including ‘compare’.

Prelude Data.List> :i Ord
class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  max :: a -> a -> a
  min :: a -> a -> a
… more info elided

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).

Prelude Data.List> compare 1 2
LT   
Prelude Data.List> compare 2 2
EQ  
Prelude Data.List> compare 3 2
GT  
Prelude Data.List> :i Ordering
data Ordering = LT | EQ | GT    -- Defined in ‘GHC.Types’

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.

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.


Best regards,
Daniel van de Ghinste


> On 22 May 2020, at 20:52, Alexander Chen <alexan...@chenjia.nl> wrote:
> 
> Hi,
> 
> maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
> 
> its in the Data.List
> 
> May 22, 2020 8:33:33 PM CEST "Daniel van de Ghinste (Lord_Luvat)" 
> <danielvandeghin...@gmail.com> wrote:
>> 
>> Hi,
>> 
>> 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:
>> Prelude> :t maximumBy
>> 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)
>> 
>> 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.
>> 
>> 
>> Best regards,
>> Daniel van de Ghinste
>> 
>>> On 22 May 2020, at 20:20, Alexander Chen <alexan...@chenjia.nl 
>>> <mailto:alexan...@chenjia.nl>> wrote:
>>> 
>>> Hi,
>>> 
>>> I want to re-write a function maximumBy (its an assignment).
>>> 
>>> However, I don't get how it works.
>>> 
>>> >maximumBy compare [1,53,9001, 10]
>>> 9001
>>> 
>>> but what does it actually do to get there? 
>>> 
>>> thanks in advance.
>>> 
>>> best,
>>> 
>>> 
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners@haskell.org <mailto:Beginners@haskell.org>
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> 
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20200522/ba7f643c/attachment-0001.html>

------------------------------

Message: 2
Date: Sat, 23 May 2020 08:02:10 +0800
From: nowsnow <nows...@163.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] MaximumBy (Alexander Chen)
Message-ID: <423640237.20200523080...@163.com>
Content-Type: text/plain; charset=utf-8

maxinumBy::(Foldable t, Ord a) => t a -> a
maxinumBy = foldr1 max

> Hi,

> I want to re-write a function maximumBy (its an assignment).

> However, I don't get how it works.

>>maximumBy compare [1,53,9001, 10]
> 9001

> but what does it actually do to get there? 

> thanks in advance.

> best,
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> <http://mail.haskell.org/pipermail/beginners/attachments/20200522/7b1cdb0d/attachment-0001.html>




------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 143, Issue 11
******************************************

Reply via email to