Daniel van de Ghinste (Lord_Luvat)
danielvandeghinste at gmail.com
Fri May 22 19:28:05 UTC 2020
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
Prelude Data.List> compare 2 2
Prelude Data.List> compare 3 2
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.
Daniel van de Ghinste
> On 22 May 2020, at 20:52, Alexander Chen <alexander at chenjia.nl> wrote:
> 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)" <danielvandeghinste at gmail.com> wrote:
>> 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 <alexander at chenjia.nl <mailto:alexander at chenjia.nl>> wrote:
>>> 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]
>>> but what does it actually do to get there?
>>> thanks in advance.
>>> Beginners mailing list
>>> Beginners at haskell.org <mailto:Beginners at haskell.org>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners