Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max algorithm haskell

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

like image 322
flatronka Avatar asked May 04 '26 06:05

flatronka


1 Answers

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

like image 105
Thomas M. DuBuisson Avatar answered May 06 '26 22:05

Thomas M. DuBuisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!