
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.

On Mon, May 05, 2003 at 02:30:18PM +1200, sashan wrote:
I came up with the following that will work for positive numbers. maxInList :: [Int]->Int maxInList [] = 0 maxInList l = foldr max 0 l
Given that you've already taken care of the empty case, (and assuming you think the maximum of an empty list is indeed zero) can't you just use foldr1 instead of foldr? (foldr1 f l is equivalent to foldr f (head l) (tail l)) Have fun in your adventures with Haskell, :) /Liyang -- .--| Liyang Hu |--| http://nerv.cx/ |--| Caius@Cam |--| ICQ: 39391385 |--. | ::::::::::::: Ack. I don't even have a signature anymore. :::::::::::: |

Liyang HU wrote:
Given that you've already taken care of the empty case, (and assuming you think the maximum of an empty list is indeed zero) can't you just use foldr1 instead of foldr? (foldr1 f l is equivalent to foldr f (head l) (tail l))
Thanks. Didn't know about foldr1 but should've thought of the definition.
Have fun in your adventures with Haskell, :) /Liyang
Yeah...I thought I'd try it again after doing a basic course on it a few years ago and liking the language. Now I want to do something practical with it. -- sashan http://sashan.netfirms.com -------------------------------------------------- Brain: Here we are, Pinky--at the dawn of time! Pinky: Narf, Brain. Wake me at the noon of time.

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 ------------------------------------------------------------------

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.
Hint: Find a better value than `0` to give as the second argument to `foldr`. Dean
participants (4)
-
Dean Herington
-
Hamilton Richards
-
Liyang HU
-
sashan