Last active
August 29, 2015 14:17
-
-
Save dsvictor94/c8cf45825242dba13968 to your computer and use it in GitHub Desktop.
Encontra a menor lista de uma lista de listas de maneira 'lazy'
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.List | |
import Data.Function | |
--comparing length lazily | |
bigger::[a] -> [a] -> Ordering | |
bigger [] [] = EQ | |
bigger (_:_) [] = GT | |
bigger [] (_:_) = LT | |
bigger (_:as) (_:bs) = bigger as bs | |
smallest::[[a]] -> [a] | |
smallest = reverse.fst.smallest'.(map ((,) [])) | |
where | |
smallest' a = case find cmp a of | |
Just l -> l | |
Nothing -> smallest' $ map helper a | |
cmp (_ , []) = True | |
cmp _ = False | |
helper::([a], [a]) -> ([a], [a]) | |
helper (as, (b:bs)) = (b:as, bs) | |
list = [[1..], [1..], [1,2,3], [1,2], [1..], [1..]] | |
main = do | |
-- print $ minimumBy (compare `on` length) list | |
-- bug. motivo: length de uma lista infinita não retornar | |
-- print $ minimumBy (bigger) list | |
-- bug. motivo: minumumBy compara 2 a 2 | |
print $ smallest list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment