uncycle s@(t:xs) | lambda == 0 = s
                 | otherwise = map fst p ++ map fst (take lambda c)
  where (p, c) = span (uncurry (/=)) . zip s $ drop lambda s
        lambda = brent 1 1 t xs
        brent _ _ _ [] = 0
        brent l p t (h:xs)
          | t == h = l
          | l == p = brent 1 (p*2) h xs
          | otherwise = brent (l+1) p t xs
uncycle s@(t:xs) | lambda == 0 = s
                 | otherwise = map fst p ++ map fst (take lambda c)
  where (p, c) = span (uncurry (/=)) . zip s $ drop lambda s
        lambda = brent 1 1 t xs
        brent _ _ _ [] = 0
        brent l p t (h:xs)
          | t == h = l
          | l == p = brent 1 (p*2) h xs
          | otherwise = brent (l+1) p t xs
uncycle s@(t:xs) | lambda == 0 = s
                 | otherwise = map fst p ++ map fst (take lambda c)
  where (p, c) = span (uncurry (/=)) . zip s $ drop lambda s
        lambda = brent 1 1 t xs
        brent _ _ _ [] = 0
        brent l p t (h:xs)
          | t == h = l
          | l == p = brent 1 (p*2) h xs
          | otherwise = brent (l+1) p t xs