<div dir="ltr">The parens around max are unnecessary:<div><br></div><div>myMax xs = foldl1 max xs</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 25, 2015 at 5:13 PM <<a href="mailto:jamb@hinojosa.com">jamb@hinojosa.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">El 2015-09-25 18:57, Francesco Ariis escribió:<br>
> On Fri, Sep 25, 2015 at 06:18:08PM -0500, <a href="mailto:jamb@hinojosa.com" target="_blank">jamb@hinojosa.com</a> wrote:<br>
>> Hello,<br>
>><br>
>> I am a complete Haskell beginner and I am doing some exercises of my<br>
>> book<br>
>> but I am stuck with the following:<br>
>><br>
>> Define<br>
>> myMax :: Ord a => [a] -> a<br>
>> which returns the maximum element of a list.<br>
>><br>
>> I must use foldl1 or foldr1 and I am given the hint to use max which<br>
>> gets<br>
>> the maximum of 2 elements.<br>
>><br>
>> I will very much appreciate if you help me solve it.<br>
><br>
> Let's say you have<br>
><br>
> foldl1 f [1,7,2,5]<br>
><br>
> where f is a binary operator (a function that 'takes two parameters').<br>
> `foldl1` will apply 1 and 7 to f, obtaining X, so we have:<br>
><br>
> X and [2,5]<br>
><br>
> then it will apply X and 2 to f, obtaining Y, so we're left with<br>
><br>
> Y and 5<br>
><br>
> and finally `f Y 5`, leading to your final result Z.<br>
><br>
> Now, if `f a b = a + b`, we would have<br>
><br>
> [1,7,2,5] -- 1+7<br>
> 8 [2,5] -- 8+2<br>
> 10 [5] -- 10+5<br>
> 15 <-- final result<br>
><br>
> But you aren't interested in the sum of the list, but its maximum.<br>
> Which operation could you use instead of (+) to achieve your goal?<br>
> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br>
<br>
<br>
<br>
<br>
I´ve got it!<br>
I tried the following successfully:<br>
<br>
myMax [] = error "List is empty."<br>
myMax xs = foldl1 (max) xs<br>
<br>
I see that my problem was in understanding the way max was applied to a<br>
list.<br>
<br>
<br>
Thanks for your kind support.<br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>