flatronka
flatronka

Reputation: 1081

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

Upvotes: 2

Views: 3104

Answers (3)

epsilonhalbe
epsilonhalbe

Reputation: 15967

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

Upvotes: 3

ceth
ceth

Reputation: 45295

(reverse $ sort [1, 6, 2, 7 ,8]) !! 0
(reverse $ sort [1, 6, 2, 7 ,8]) !! 1

Upvotes: 1

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

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

Upvotes: 6

Related Questions