[Haskell-beginners] Removing the biggest element from a list - maybe slow?

Nathan Huesken haskell at lonely-star.org
Mon May 24 08:42:28 EDT 2010


Hi,

I want to remove the biggest element from a list:

withoutBiggest (x:xs) =
  withoutBiggestImpl (biggest x xs) [] (x:xs)
    where 
      biggest :: (Ord a) => a -> [a] -> a  
      biggest big [] = big
      biggest big (x:xs) =
        if x > big then
          biggest x xs
        else
          biggest big xs
      withoutBiggestImpl :: (Eq a) => a -> [a] -> [a] -> [a]
      withoutBiggestImpl big before (x:xs) =
        if big == x then
          before ++ xs
        else
          withoutBiggestImpl big (before ++ [x]) xs


Works, but I am a little concerned that this is
slower than needed, because the list has to be iterated twice.

Can this be done faster?

Regards,
Nathan


More information about the Beginners mailing list