Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2013 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7335362 to your computer and use it in GitHub Desktop.
Save anonymous/7335362 to your computer and use it in GitHub Desktop.
module Wtf where
import Control.Applicative ((<$))
import Data.List (unfoldr)
(.:) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
(.:) = fmap . fmap
shorter :: [a] -> [b] -> Bool
shorter xs ys = (() <$ xs) < (() <$ ys)
iter :: (Eq a, Num a) => [a] -> [a] -> [a]
iter xs ys = dropWhile (== 0) $ zipWith (abs .: (-)) xs (ys ++ repeat 0)
-- >>> wtf [1,1,0,1,0] [1,1,1]
-- [[1,1,0,1,0],[1,1,0],[1]]
wtf :: (Eq a, Num a) => [a] -> [a] -> [[a]]
wtf xs ys = xs : unfoldr (\xs' ->
if shorter xs' ys
then Nothing
else let xs'' = iter xs' ys in Just (xs'', xs'')) xs
-- >>> foo [1,1,0,1,0] [1,1,1]
-- [1]
foo :: (Eq a, Num a) => [a] -> [a] -> [a]
foo = last .: wtf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment