Last active
April 26, 2018 15:26
-
-
Save scmu/900cc6b30972d2b55bf9cfc8803d8c3c to your computer and use it in GitHub Desktop.
One-Liner nub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Ord | |
import Data.List | |
nubSimple :: [Int] -> [Int] | |
nubSimple = map head . group . sort | |
nubStable :: [Int] -> [Int] | |
nubStable = map fst . sortBy (comparing snd). map head . | |
groupBy ((. fst).(==).fst) . sort . flip zip [0..] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question: Whether the spec of nub requires to maintain the order of elements?
As in module Data.List
nub [1,2,3,48,6,4,3,2] ==> [1,2,3,48,6,4]
By the way, nub :: Eq a => [a] -> [a], not "Ord a"
so sort :: Ord a => [a] -> [a] may not work on type a.