Maximum value in a list

Hamilton Richards
Sun, 4 May 2003 21:56:21 -0500

At 2:30 PM +1200 5/5/03, sashan wrote:
>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

No, max has no identity, and so maxInList has no correct definition 
for an empty list. The shortest lists for which it is defined are 
lists of one element.

The Standard Prelude contains a close relative of foldr called 
foldr1. It's defined for just this situation.

Hamilton Richards                Department of Computer Sciences
Senior Lecturer                  The University of Texas at Austin
512-471-9525                     1 University Station C0500
Taylor Hall 5.138                Austin, Texas 78712-1188