Maximum value in a list

Garner, Robin Robin.Garner@crsrehab.gov.au
Mon, 5 May 2003 12:51:04 +1000


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C312B1.2BC6B520
Content-Type: text/plain;
	charset="iso-8859-1"

You'll find in the Prelude that Int is an instance of Bounded, so minBound
and maxBound are defined, and are the identities you need.

cheers

-----Original Message-----
From: sashan [mailto:sashang@ihug.co.nz]
Sent: Monday, May 05, 2003 12:30 PM
To: haskell-cafe@haskell.org
Subject: Maximum value in a list


Hi

I was reading through the tutorial Yet Another Haskell Tutorial and 
doing chapter 3 ex 5, write a function using foldr and max to return the 
maximum value in a list.

I came up with the following that will work for positive numbers.

maxInList :: [Int]->Int
maxInList [] = 0
maxInList l = foldr max 0 l

Is there an identity for the max function? Because currently if the list 
  [-1,-3,-4] is passed to maxInList it will return 0.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

------_=_NextPart_001_01C312B1.2BC6B520
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2654.89">
<TITLE>RE: Maximum value in a list</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>You'll find in the Prelude that Int is an instance of =
Bounded, so minBound and maxBound are defined, and are the identities =
you need.</FONT></P>

<P><FONT SIZE=3D2>cheers</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: sashan [<A =
HREF=3D"mailto:sashang@ihug.co.nz">mailto:sashang@ihug.co.nz</A>]</FONT>=

<BR><FONT SIZE=3D2>Sent: Monday, May 05, 2003 12:30 PM</FONT>
<BR><FONT SIZE=3D2>To: haskell-cafe@haskell.org</FONT>
<BR><FONT SIZE=3D2>Subject: Maximum value in a list</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>Hi</FONT>
</P>

<P><FONT SIZE=3D2>I was reading through the tutorial Yet Another =
Haskell Tutorial and </FONT>
<BR><FONT SIZE=3D2>doing chapter 3 ex 5, write a function using foldr =
and max to return the </FONT>
<BR><FONT SIZE=3D2>maximum value in a list.</FONT>
</P>

<P><FONT SIZE=3D2>I came up with the following that will work for =
positive numbers.</FONT>
</P>

<P><FONT SIZE=3D2>maxInList :: [Int]-&gt;Int</FONT>
<BR><FONT SIZE=3D2>maxInList [] =3D 0</FONT>
<BR><FONT SIZE=3D2>maxInList l =3D foldr max 0 l</FONT>
</P>

<P><FONT SIZE=3D2>Is there an identity for the max function? Because =
currently if the list </FONT>
<BR><FONT SIZE=3D2>&nbsp; [-1,-3,-4] is passed to maxInList it will =
return 0.</FONT>
</P>

<P><FONT =
SIZE=3D2>_______________________________________________</FONT>
<BR><FONT SIZE=3D2>Haskell-Cafe mailing list</FONT>
<BR><FONT SIZE=3D2>Haskell-Cafe@haskell.org</FONT>
<BR><FONT SIZE=3D2><A =
HREF=3D"http://www.haskell.org/mailman/listinfo/haskell-cafe" =
TARGET=3D"_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</=
A></FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C312B1.2BC6B520--