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
First off, you probably want to give explicit type signatures because this isn’t saying what you might think it’s saying.
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, ismaximum (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].