haskell - Why does "nub" have O(n^2) complexity when it could be O(n)? -


the nub function data.list has o(n2) complexity. it's clear implementing o(n) algorithm doable , not hard. why doesn't haskell it?

 nub :: eq => [a] -> [a] base data.list o(n^2). nub function removes duplicate elements list. in particular, keeps first occurrence of each element. (the name nub means `essence'.) special case of nubby, allows programmer supply own equality test.  

to provide answer has been made rather painfully explicit in comments: premise wrong, nub :: eq => [a] -> [a] can not have o(n) implementation.

you thinking of implementation can assume ordering, ordnub :: ord => [a] -> [a], linear log. or perhaps assuming hashable, bucket-sortable sort of thing. unlike eq, when have ordering information don't need potentially compare every pair of elements.

this topic discussed in 2008: http://haskell.1045720.n5.nabble.com/ghc-2717-add-nubwith-nubord-td3159919.html

this topic discussed again, different actors in community, in 2013: https://www.haskell.org/pipermail/haskell-cafe/2013-october/110984.html


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -