Skip to content

Instantly share code, notes, and snippets.

@dsvictor94
Last active August 29, 2015 14:17
Show Gist options
  • Save dsvictor94/c8cf45825242dba13968 to your computer and use it in GitHub Desktop.
Save dsvictor94/c8cf45825242dba13968 to your computer and use it in GitHub Desktop.
Encontra a menor lista de uma lista de listas de maneira 'lazy'
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