[Haskell-cafe] nub vs. find + (:) Is this abysmal code?

Michael Feathers mfeathers at mindspring.com
Sun Feb 10 10:07:01 EST 2008


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


My guess is that the latter is more efficient, but then when I think 
about laziness, I wonder whether the first is a fair trade.

Michael


More information about the Haskell-Cafe mailing list