Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Get max element of a list using foldl or foldr
([email protected])
2. Re: Get max element of a list using foldl or foldr
(Francesco Ariis)
3. Re: Get max element of a list using foldl or foldr
([email protected])
4. Re: Get max element of a list using foldl or foldr
(Rein Henrichs)
----------------------------------------------------------------------
Message: 1
Date: Fri, 25 Sep 2015 18:18:08 -0500
From: [email protected]
To: [email protected]
Subject: [Haskell-beginners] Get max element of a list using foldl or
foldr
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed
Hello,
I am a complete Haskell beginner and I am doing some exercises of my
book but I am stuck with the following:
Define
myMax :: Ord a => [a] -> a
which returns the maximum element of a list.
I must use foldl1 or foldr1 and I am given the hint to use max which
gets the maximum of 2 elements.
I will very much appreciate if you help me solve it.
Thanks in advance,
JAMB
------------------------------
Message: 2
Date: Sat, 26 Sep 2015 01:57:32 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Get max element of a list using foldl
or foldr
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Sep 25, 2015 at 06:18:08PM -0500, [email protected] wrote:
> Hello,
>
> I am a complete Haskell beginner and I am doing some exercises of my book
> but I am stuck with the following:
>
> Define
> myMax :: Ord a => [a] -> a
> which returns the maximum element of a list.
>
> I must use foldl1 or foldr1 and I am given the hint to use max which gets
> the maximum of 2 elements.
>
> I will very much appreciate if you help me solve it.
Let's say you have
foldl1 f [1,7,2,5]
where f is a binary operator (a function that 'takes two parameters').
`foldl1` will apply 1 and 7 to f, obtaining X, so we have:
X and [2,5]
then it will apply X and 2 to f, obtaining Y, so we're left with
Y and 5
and finally `f Y 5`, leading to your final result Z.
Now, if `f a b = a + b`, we would have
[1,7,2,5] -- 1+7
8 [2,5] -- 8+2
10 [5] -- 10+5
15 <-- final result
But you aren't interested in the sum of the list, but its maximum.
Which operation could you use instead of (+) to achieve your goal?
------------------------------
Message: 3
Date: Fri, 25 Sep 2015 19:10:58 -0500
From: [email protected]
To: [email protected]
Subject: Re: [Haskell-beginners] Get max element of a list using foldl
or foldr
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
El 2015-09-25 18:57, Francesco Ariis escribi?:
> On Fri, Sep 25, 2015 at 06:18:08PM -0500, [email protected] wrote:
>> Hello,
>>
>> I am a complete Haskell beginner and I am doing some exercises of my
>> book
>> but I am stuck with the following:
>>
>> Define
>> myMax :: Ord a => [a] -> a
>> which returns the maximum element of a list.
>>
>> I must use foldl1 or foldr1 and I am given the hint to use max which
>> gets
>> the maximum of 2 elements.
>>
>> I will very much appreciate if you help me solve it.
>
> Let's say you have
>
> foldl1 f [1,7,2,5]
>
> where f is a binary operator (a function that 'takes two parameters').
> `foldl1` will apply 1 and 7 to f, obtaining X, so we have:
>
> X and [2,5]
>
> then it will apply X and 2 to f, obtaining Y, so we're left with
>
> Y and 5
>
> and finally `f Y 5`, leading to your final result Z.
>
> Now, if `f a b = a + b`, we would have
>
> [1,7,2,5] -- 1+7
> 8 [2,5] -- 8+2
> 10 [5] -- 10+5
> 15 <-- final result
>
> But you aren't interested in the sum of the list, but its maximum.
> Which operation could you use instead of (+) to achieve your goal?
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
I?ve got it!
I tried the following successfully:
myMax [] = error "List is empty."
myMax xs = foldl1 (max) xs
I see that my problem was in understanding the way max was applied to a
list.
Thanks for your kind support.
------------------------------
Message: 4
Date: Sat, 26 Sep 2015 03:31:43 +0000
From: Rein Henrichs <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Get max element of a list using foldl
or foldr
Message-ID:
<cajp6g8wfjh2_thydjoqg80_aeytcbiqmjsafhnnzhfn6z7w...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
The parens around max are unnecessary:
myMax xs = foldl1 max xs
On Fri, Sep 25, 2015 at 5:13 PM <[email protected]> wrote:
> El 2015-09-25 18:57, Francesco Ariis escribi?:
> > On Fri, Sep 25, 2015 at 06:18:08PM -0500, [email protected] wrote:
> >> Hello,
> >>
> >> I am a complete Haskell beginner and I am doing some exercises of my
> >> book
> >> but I am stuck with the following:
> >>
> >> Define
> >> myMax :: Ord a => [a] -> a
> >> which returns the maximum element of a list.
> >>
> >> I must use foldl1 or foldr1 and I am given the hint to use max which
> >> gets
> >> the maximum of 2 elements.
> >>
> >> I will very much appreciate if you help me solve it.
> >
> > Let's say you have
> >
> > foldl1 f [1,7,2,5]
> >
> > where f is a binary operator (a function that 'takes two parameters').
> > `foldl1` will apply 1 and 7 to f, obtaining X, so we have:
> >
> > X and [2,5]
> >
> > then it will apply X and 2 to f, obtaining Y, so we're left with
> >
> > Y and 5
> >
> > and finally `f Y 5`, leading to your final result Z.
> >
> > Now, if `f a b = a + b`, we would have
> >
> > [1,7,2,5] -- 1+7
> > 8 [2,5] -- 8+2
> > 10 [5] -- 10+5
> > 15 <-- final result
> >
> > But you aren't interested in the sum of the list, but its maximum.
> > Which operation could you use instead of (+) to achieve your goal?
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
>
>
> I?ve got it!
> I tried the following successfully:
>
> myMax [] = error "List is empty."
> myMax xs = foldl1 (max) xs
>
> I see that my problem was in understanding the way max was applied to a
> list.
>
>
> Thanks for your kind support.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150926/f67fdae2/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 87, Issue 18
*****************************************