Discussion:
Maximum value in a list
sashan
2003-05-05 02:30:18 UTC
Permalink
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.
Liyang HU
2003-05-05 02:51:19 UTC
Permalink
Post by sashan
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/ |--| ***@Cam |--| ICQ: 39391385 |--.
| ::::::::::::: Ack. I don't even have a signature anymore. :::::::::::: |
sashan
2003-05-05 03:19:55 UTC
Permalink
Post by Liyang HU
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.
Post by Liyang HU
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.
Garner, Robin
2003-05-05 02:51:04 UTC
Permalink
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:***@ihug.co.nz]
Sent: Monday, May 05, 2003 12:30 PM
To: haskell-***@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.
Hamilton Richards
2003-05-05 02:56:21 UTC
Permalink
Post by sashan
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
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
***@cs.utexas.edu ***@swbell.net
------------------------------------------------------------------
Dean Herington
2003-05-05 14:23:38 UTC
Permalink
Post by sashan
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

Loading...