Last active
December 17, 2015 23:59
-
-
Save WillNess/5692963 to your computer and use it in GitHub Desktop.
nubBy's correctness and efficiency in ( nubBy (((==0).).rem) [2..] ) see also: https://ideone.com/ZGJBLZ
This file contains hidden or 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
| nubBy (((==0).).rem) $ [2..] -- (\a b-> rem a b == 0) -- a > b | |
| tsieve (x:xs) = x : tsieve (filter (\a b-> not (rem a b == 0)) xs) -- a > b | |
| -- http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Data-List.html#nubBy | |
| -- | The 'nubBy' function behaves just like 'nub', except it uses a | |
| -- user-supplied equality predicate instead of the overloaded '==' | |
| -- function. | |
| nubBy :: (a -> a -> Bool) -> [a] -> [a] | |
| #ifdef USE_REPORT_PRELUDE -- calls eq EARLIER LATER ---**** | |
| nubBy eq [] = [] -- should be: not (eq y x) -- !! y > x !! | |
| nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs) -- tested by smaller first: | |
| -- GOOD order of tests, WRONG order of args | |
| #else | |
| nubBy eq l = nubBy' l [] -- input prior_uniques | |
| where | |
| nubBy' [] _ = [] | |
| nubBy' (y:ys) xs -- (y:ys) = input ; xs = prior_uniques | |
| | elem_by eq y xs = nubBy' ys xs -- y = most recent input ; xs = prior_uniques | |
| | otherwise = y : nubBy' ys (y:xs) -- => y > xs => (y:xs) is DESCENDING ORDER!! | |
| -- Not exported: | |
| -- Note that we keep the call to `eq` with arguments in the see also: | |
| -- same order as in the reference implementation https://ideone.com/ZGJBLZ | |
| -- 'xs' is the list of things we've seen so far, | |
| -- 'y' is the potential new element | |
| elem_by :: (a -> a -> Bool) -> a -> [a] -> Bool -- calls eq LATER EARLIER ---**** | |
| elem_by _ _ [] = False | |
| elem_by eq y (x:xs) = y `eq` x || elem_by eq y xs -- eq y x, y > x !! CORRECT order of args | |
| #endif -- BUT ((x:xs) is descending, so) BAD ORDER of tests !!!!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment