[Haskell-cafe] nub vs. find + (:) Is this abysmal code?
Henning Thielemann
lemming at henning-thielemann.de
Sun Feb 10 11:02:18 EST 2008
On Sun, 10 Feb 2008, Michael Feathers wrote:
> How bad is this:
>
> addProduct :: [Product] -> Product -> [Product]
> addProduct inventory product = nub (product : inventory)
>
>
> compared to this:
>
> addProduct :: [Product] -> Product -> [Product]
> addProduct inventory p
> | isNothing (find (==p) inventory) = p : inventory
> | otherwise = inventory
Data.Set is first choice, 'elem' is second choice, but still better than
'isNothing (find ...)'.
More information about the Haskell-Cafe
mailing list