2

The following algorithm has some bugs, i don't know how to fix it, i try but i can't fix it.

This algorithm return the 1nd and 2nd max value from a list.

Thanks for help.

maxmimum [] = []  
maxmimum [head] = [head] 
maxmimum [head1 : head2 : maradek] 
  | head1 > head2 = maxmimum2 maradek head1 head2 
  | otherwise = maxmimum2 maradek head2 head1

--maxmimum2 :: [Int] Int Int -> [Int]

maxmimum2 [] head1 head2 = [head1, head2] 
maxmimum2 [head : maradek] head1 head2    
  | head > head1 = maxmimum2 maradek head head1
  | head > head2 = maxmimum2 maradek head1 head
  | otherwise = maxmimum2 maradek head1 head2

Parse error in pattern: maxmimum

3 Answers 3

6

First off, you probably want to give explicit type signatures because this isn't saying what you might think it's saying.

maxmimum [head1 : head2 : maradek] 

That says you have a list of lists. The outter list has a single element, which is itself a list of length at least two. The type is [[a]]. What you want, I expect, is maximum (head1 : head2 : restOfList) = ....

The same issue appears in maximum2 ([head : maradek] should be (head : maradek)). The type signature you have commented out for maximum2 is almost right (once you use this fix), you just need to add in the arrows (the function is curried): maximum2 :: [Int] -> Int -> Int -> [Int].

Sign up to request clarification or add additional context in comments.

3 Comments

To reiterate, [head1 : head2 : ... ] is the same as [ [head1, head2, ...] ] while (head1 : head2 : ...) is [head1, head2, ...]. The : operator builds up a list and the surrounding [] builds another list.
"maxmimum2 :: [Int] Int Int -> [Int]" this is important for the code? or just better to understand?
The type signatures can cause changes in the generated code, but not usually and not in ways you should concern yourself with right now. It is usually about documentation and helps you get better error messages (if your code says one thing and your type signature says another then the compiler will be able to give you an error for that function, instead of an error when you apply that function to the wrong type).
3

First please write down type signatures like

maximum :: Ord a => [a] -> a

it makes thinking about functions way more easily. so in this case we have a function that takes a list of comparable things (Ord a => [a]) and gives us one - the maximum of them.

maximum :: Ord a => [a] -> a
maximum [] = undefined
maximum [x] = x
maximum (x1 : x2 : xs)
  | x1 > x2 = maximum (x1 : xs)
  | otherwise = maximum (x2 : xs)

Secondly name things so that others are not confused - i don't know what a maradek is or could be, in haskell often "list of x" is denoted by xs.

now let's test our maximum-function:

> ghci maximum.hs
>>> maximum []
*** Exception: Prelude.maximum: empty list
>>> maximum [1..10]
    10
>>> maximum [10,9..1]
    10
>>> maximum "alpha,beta,gamma"
    't'
>>> maximum [1.0,1.1..10.0]
    10.000000000000053

works all as expected: error on emptylist, maximum on integers and characters as well only the last one is unexpected as it is one of the oddities of working with floating point numbers and rounding errors.

Edit

After the comment I misread the op's question (I'm verry sorry for that!).

maximum2 :: Ord a => [a] -> (a,a)
maximum2 [] = undefined
maximum2 [x] = undefined
maximum2 [x1, x2] = (x1, x2)
maximum2 (x1 : x2 : x3 : xs)
  | x2 > x1 = maximum2 (x2 : x1 : x3 : xs)
  | x3 > x1 = maximum2 (x3 : x1 : xs)
  | x3 > x2 = maximum2 (x1 : x3 : xs)
  | otherwise = maximum2 (x1 : x2 : xs)

so here is the expected solution I hope.

> ghci maximum.hs
>>> maximum []
*** Exception: Prelude.maximum: empty list
>>> maximum2 [1..10]
    (10,9)
>>> maximum2 [10,9..1]
    (10,9)
>>> maximum2 [9,19,10,23]
    (23,19)
>>> maximum "alpha,beta,gamma"
    ('t','p')
>>> maximum [1.0,1.1..10.0]
    (10.000000000000053,9.900000000000052)
>>> maximum2 (10:[1..10])
    (10,10)

I don't know if the last result is as expected or if there should be (10,9).

2 Comments

i actually just copied your version and tidied it up a bit.
However, this doesn't produce the two highest numbers, which is apparently the problem OP was solving.
1
(reverse $ sort [1, 6, 2, 7 ,8]) !! 0
(reverse $ sort [1, 6, 2, 7 ,8]) !! 1

2 Comments

If he's able to leverage larger library functions then you're right, just do take 2 . reverse . sort, but this is homework and he did have basically the correct answer. I think we should encourage him to continue developing what he has and understand what is wrong.
I just want to say it is easier to sort the list and get the first two member. He can sort by library function or create own sort function - it doesn't matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.