
At 2:30 PM +1200 5/5/03, sashan wrote:
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
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. --HR -- ------------------------------------------------------------------ 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 ham@cs.utexas.edu hrichrds@swbell.net ------------------------------------------------------------------