
hello, I'm new to haskell, I'm studying for an exam, and I'm having trouble with this exercise. I have a list of Integers, and I want to return the highest value of all. i have this: comp :: Int -> Int -> Int comp x y | x < y = y | otherwise = x maxList :: [Int] -> [Int] maxList (x1:x2:xs) = comp x1 x2 : maxList xs maxList [] = [] this code returns a list, but I only want it to return a single Int... but can't seem to do it, could you please help me? --------------------------------- Do you Yahoo!? Yahoo! News - Today's headlines

Hi,
I have a list of Integers, and I want to return the highest value of all.
i have this: comp :: Int -> Int -> Int comp x y | x < y = y | otherwise = x
This looks fine.
maxList :: [Int] -> [Int]
This type is probably wrong. If you are only trying to find the single greatest integer, why are you returning a list? Possibly to handle the case when the input list is empty, but a 'Maybe' might be better suited for this (or simply throwing an error).
maxList (x1:x2:xs) = comp x1 x2 : maxList xs maxList [] = []
You should probably try to think inductively about this problem. Suppose you're given an empty list. What is the maximum value of this list? Well, nothing. This should be an error. Now, suppose you're given a list with only one element. Obviously this single element is the maximum of the list, so you should return it. Suppose you have more than one element. How does the maximum value of the entire list relate to the maximum value of the tail of the list and the head of the list (inductively)? Given that answer, you should be able to write a three line solution. Now, for extra credit, write a one line solution using a fold and your comp function. - Hal (note that your 'comp' function is the standard prelude 'max' function, for the most part, and that your 'maxList' is the prelude 'maximum' function.)
participants (2)
-
Hal Daume III
-
Nuno Silva