[Haskell-cafe] Time consumption nub

Stuart Cook scook0 at gmail.com
Wed Jul 18 08:40:24 EDT 2007


On 7/18/07, Arie Groeneveld <bradypus at xs4all.nl> wrote:
> Ok, so when do I use nub instead of 'map head.group.sort' ?
>
> Using nub gave me a lot of trouble in terms of time consumption
> while handling long lists.

Well, nub is non-strict, so you can use it on infinite or partial
lists, provided you don't consume too much of the result.

e.g.

  Prelude Data.List> take 10 $ nub [1..]
  [1,2,3,4,5,6,7,8,9,10]

  Prelude Data.List> take 10 $ map head . group . sort $ [1..]
  Interrupted.

(Yes, taking nub of [1..] is silly; it's just an example.)


Stuart


More information about the Haskell-Cafe mailing list