MaximumBy

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

MaximumBy

Alexander Chen
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: MaximumBy

Daniel van de Ghinste (Lord_Luvat)
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: MaximumBy

Alexander Chen
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)" <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners





_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: MaximumBy

Francesco Ariis
In reply to this post by Alexander Chen
Hello Alexander,

Il 22 maggio 2020 alle 20:20 Alexander Chen ha scritto:

> 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? 

Do you know how to use foldr and friends?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: MaximumBy

Ut Primum
In reply to this post by Alexander Chen
Hi,
if it is ok for you to work only with lists (not generic foldables), you could write it this way:

maximumBy cmp [x] = x
maximumBy cmp (x:y:xs) = if (cmp x y)==GT then maximumBy cmp (x:xs) else maximumBy cmp (y:xs)


the signature is 
maximumBy :: (a -> a -> Ordering) -> [a] -> a

Ut



Mail priva di virus. www.avg.com

Il giorno ven 22 mag 2020 alle ore 20:53 Alexander Chen <[hidden email]> ha scritto:
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)" <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Mail priva di virus. www.avg.com

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: MaximumBy

Daniel van de Ghinste (Lord_Luvat)
In reply to this post by Alexander Chen
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 <[hidden email]> 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)" <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners






_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners